简体   繁体   English

MySQL在一个查询中将2个表连接在一起

[英]MySQL joining 2 tables together in one query

I've got 2 queries which I want to join and I've never really done it before. 我有2个要加入的查询,而我之前从未真正做到过。 Hopefully one of you bright sparks can help. 希望你们中的一员能帮助您。

It's displaying businesses for sale. 它正在显示要出售的业务。 The first query is fine. 第一个查询很好。

SELECT DISTINCT * FROM businesses WHERE business_asking_price > 1 AND business_location = 11 ORDER BY business_id DESC

The above searches for all businesses within a certain price as well as finding all the businesses in London. 上面的代码搜索了某个价格范围内的所有企业,并找到了伦敦的所有企业。

So far so good. 到现在为止还挺好。

But the headache I'm having is the TYPE of business. 但是我头疼的是业务类型。 One business (let's say a hairdressers) can have multiple types of business type categories. 一个企业(例如理发店)可以具有多种类型的企业类型。 In this case it's Health/Beauty and Hair Salons. 在这种情况下,它是健康/美容和美发沙龙。

So the categories are in a different table called bus_parents: 因此,类别位于另一个称为bus_parents的表中:

bus_parent_id  |   bus_parent_parent | bus_parent_child

176            |          56         |         42
177            |          56         |         43
181            |          56         |         46
202            |          56         |         57

bus_parent_id is obviously the unique id bus_parent_id显然是唯一的ID

bus_parent_parent is the business type id bus_parent_parent是业务类型ID

bus_parent_child is the business id bus_parent_child是公司编号

So i really want to wrap the two queries together so it finds all the businesses with the first query, then narrows them down further so it only shows businesses within a bus_parent_parent as well. 因此,我真的很想将两个查询包装在一起,以便它在第一个查询中找到所有业务,然后将其进一步缩小,因此它也只显示bus_parent_parent中的业务。

Hopefully I've made myself clear as to what I'm trying to do. 希望我已经清楚我要做什么。

Is it a JOIN that I'm after or something else? 是我追随的JOIN还是其他?

Try below SQL with JOIN: 在下面的SQL中尝试使用JOIN:

SELECT DISTINCT businesses.* 
FROM businesses 
INNER JOIN bus_parents ON businesses.business_id = bus_parents.bus_parent_child 
WHERE businesses.business_asking_price > 1 
AND businesses.business_location = 11 
AND bus_parents.bus_parent_parent = 56 
ORDER BY businesses.business_id DESC

It will retrieve all the businesses, whose type id = 56, you can change this parameter according to your need. 它将检索所有类型为id = 56的企业,您可以根据需要更改此参数。

Based on your description for each field in bus_parents I have used JOIN to retrieve the desired result. 根据您对bus_parents中每个字段的描述,我已使用JOIN检索所需的结果。 (narrow down on business type id) (缩小业务类型ID)

If i've understood you correctly, I think you want something like this: 如果我正确理解了您,我认为您需要这样的东西:

SELECT DISTINCT * FROM businesses 
LEFT JOIN business_type ON business_type.bus_parent_child = business.id 
WHERE business_asking_price > 1 AND business_location = 11 
AND business_type.bus_parent_parent = <Your desired parent> 
ORDER BY business_id DESC

This is joining business to business_type on business_type.bus_parent_child = business.id 这是将业务加入到business_type.bus_parent_child = business.id

With an additional WHERE clause on the type.bus_parent_parent type.bus_parent_parent上带有附加的WHERE子句

SELECT DISTINCT businesses.* 
FROM businesses a
INNER JOIN bus_parents b ON a.business_id = b.bus_parent_parent 
WHERE a.business_asking_price > 1 
AND a.business_location = 11 
ORDER BY a.business_id DESC
SELECT * FROM businesses b WHERE business_asking_price > 1 AND business_location = 11 

and (select bus_parent_id from bus_parents where bus_parent_child=b.bussiness_id and bus_parent_parent=in(56,57,58) limit 1) != NULL

ORDER BY business_id DESC

here i assume bussiness_id is a primary key column of businesses table. 在这里,我假设bussiness_id是业务表的主键列。

You should avoid using distinct on large queries as it can be quite taxing(slow) on large tables. 您应该避免在大型查询上使用distinct,因为在大型表上这样做可能会很费力(缓慢)。 especially if you request many columns. 特别是如果您请求许多列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM