简体   繁体   English

SQL使用JOIN选择全部

[英]SQL select all using JOIN

I'm using this query to collate two sets of results but I now need to use JOIN instead of UNION to get the second part of the data from another table. 我正在使用此查询来整理两组结果,但是现在我需要使用JOIN而不是UNION来从另一个表中获取数据的第二部分。

However I need quite a lot of fields and can't seem to find a way to maintain the use of SELECT * when using JOIN. 但是我需要很多字段,并且在使用JOIN时似乎找不到找到维持SELECT *使用的方法。

mysql_query("SELECT * FROM table.products WHERE category='$cat'  GROUP BY product_id ORDER BY id UNION ALL SELECT * FROM table.products WHERE  type='red' GROUP BY product_id ");

Table - products 表-产品

product_id | title    | category | id
0            one         home      10
1            two         home      11
1            two - a     home      12
2            three       work      13

Table - product_details 表格-product_details

product_id | type | size |
0            blue   S
1            blue   M
1            red    L

Ultimately I need to list every product in the first table for a given category eg home, as there is sometimes two entries or more for a single product id, I need to only select one row for each product id value. 最终,我需要在第一个表中为给定类别(例如家庭)列出每个产品,因为有时一个商品ID会有两个或更多条目,所以我只需要为每个商品ID值选择一行。 I also need to join the second table so I can get the size info, however I must be able to get the size info by preferring a type eg red. 我还需要加入第二张表,以便获得尺寸信息,但是我必须能够通过偏爱红色等类型来获得尺寸信息。

So for this example I would get a list like: 因此,对于此示例,我将获得类似以下的列表:

 product_id  | title    | category | type | size
 0             one        home       blue    S
 1             two        home       red     L

This excludes product_id 2 as it's not in the home category, the first entry for product_id equaling 1 is selected because of the GROUP BY and ORDER BY and the information on size for product_id 1 is L because it is of type red not blue. 这不包括product_id 2,因为它不在家庭类别中,因为GROUP BY和ORDER BY选择了product_id等于1的第一个条目,并且product_id 1的尺寸信息为L,因为它的类型为红色而不是蓝色。

Your query can be simplified like below since you are using the same table table.products . 由于您使用的是同一表table.products因此可以像下面那样简化查询。 Not sure why you need to UNION them. 不知道为什么需要UNION

SELECT * FROM table.products 
WHERE category='$cat' 
and type='red'
GROUP BY product_id  

EDIT: 编辑:

With your edited post, the query should look like 编辑完帖子后,查询应如下所示

select p.product_id,p.title,p.category,q.type,q.size
from products p join product_details q
on p.product_id = q.product_id
where p.category =  'home'
and q.type = 'red'

Assuming you are using MySQL, you want a join with an aggregation or aggressive filtering. 假设您正在使用MySQL,则希望通过聚合或主动过滤进行联接。 Here is an example using join and aggregation : 这是使用joinaggregation的示例:

select p.product_id, p.title, p.category,
       substring_index(group_concat(pd.type order by pd.type = 'red' desc, pd.type), ',', 1) as type,
       substring_index(group_concat(pd.size order by pd.type = 'red' desc, pd.type), ',', 1) as size
from products p join
     product_details pd
     on p.product_id = qpd.product_id
where p.category =  'home'
group by p.product_id;

The expression substring_index(group_concat(. . .)) is choosing one type (and one size ) with precedence given to the red type. 表达式substring_index(group_concat(. . .))选择一种type (和一种size ),并优先使用red类型。

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

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