简体   繁体   English

MySQL从两个表中进行多重选择(过滤)

[英]mySQL multiple select from two tables (filtering)

I have two tables at my DB that i want to make a select query First one keeps the product id (int) and if the product is published The second table keeps the filters, and matches with id_product (which is basicaly the product_id from Products) . 我的数据库中有两个表,我要进行选择查询。第一个表保留产品ID(int),如果产品已发布,第二个表保留过滤器,并与id_product匹配(基本上是Products的product_id) 。 at id_filter_child we have the value 在id_filter_child我们有值

products 产品展示

product_id,
published

filters 过滤器

id_pro_fil,
id_product,
id_filter_child

My tables now are: 我的表现在是:

products 产品展示

1     yes
2     yes
3     no

filters 过滤器

10   1    4
11   1    5
12   2    4
13   2    6
14   2    7

So that is my DB at the moment. 所以这就是我的数据库。 From input i have the filters that i want to search So i want to select all products with filter=4 i use: 从输入中,我有要搜索的过滤器,所以我想选择filter = 4的所有产品,我使用:

SELECT * FROM products,filters
WHERE products.product_id=filters.id_product
     AND products.published='1'
     AND filters.id_filter_child='4'

That one works! 那个工程!

Now i need to select all products with filter 4 AND 6. I use: 现在,我需要选择所有带有过滤器4和6的产品。我使用:

SELECT * FROM products,filters
WHERE products.product_id=filters.id_product
     AND products.published='1'
     AND filters.id_filter_child='4'
     AND filters.id_filter_child='6'

but i doesnt work (as a result i would want the product with id=2) 但我无法正常工作(因此,我想要ID = 2的产品)

Some help please 请帮忙

Thanks in advance 提前致谢

Try this: 尝试这个:

SELECT p.*
FROM products p
LEFT JOIN filters ON p.product_id = f.id_product
WHERE p.published='1' AND f.id_pro_fil IN (4,6)

Consider your logic: 考虑一下您的逻辑:

 AND filters.id_filter_child='4'
 AND filters.id_filter_child='6'

You're requiring every filter_child in the database to have two DIFFERENT values at the same time. 您要求数据库中的每个filter_child都必须同时具有两个DIFFERENT值。 "this record must be a banana and a ferrari simutaneously" “此记录必须同时是香蕉和法拉利”

You want an OR instead: 您需要一个OR

AND ((filter_child = '4') OR (filter_child = '6'))

(note the extra brackets), or more compactly: (请注意多余的括号),或者更紧凑一些:

AND (filter_child IN ('4', '6'))

--- comment followup ---评论跟进

If you need both values to be attached to a record,t hen you'll have to use some extra logic: 如果需要将两个值都附加到记录中,则必须使用一些额外的逻辑:

SELECT *, COUNT(filters.id_filter_child IN ('4', '6')) AS count
FROM products,filters
WHERE products.product_id=filters.id_product
     AND products.published='1'
     AND filters.id_filter_child IN ('4', '6')
GROUP BY filters.id_filter_child
HAVING count = 2

The where returns only the records that have either 4 or 6 for filter_child - this will catch records that have at least ONE of those two values. where仅返回filter_child具有46的记录-这将捕获具有两个值中至少一个的记录。 The COUNT(..) + GROUP + HAVING business will then filter out all but those records that have BOTH of the values. 然后, COUNT(..) + GROUP + HAVING业务将过滤掉除具有两个值之外的所有记录。

You can use OR or IN 您可以使用OR或IN

SELECT * FROM products,filters 
  WHERE products.product_id=filters.id_product 
      AND  products.published='1' 
      AND (filters.id_filter_child='4' OR filters.id_filter_child='6')

SELECT * FROM products,filters 
  WHERE products.product_id=filters.id_product 
      AND  products.published='1' 
      AND filters.id_filter_child IN (4,6)

you should OR in between the two conditions: 您应该在以下两种情况之间进行“ 或”操作

SELECT * FROM products,filters
WHERE products.product_id=filters.id_product
     AND products.published='1'
     AND filters.id_filter_child='4'
     OR filters.id_filter_child='6'

but to make your sql smaller and simpler you can use IN operator also: 但是要使您的sql更小和更简单,您也可以使用IN运算符:

SELECT * FROM products,filters
WHERE products.product_id=filters.id_product
     AND products.published='1'
     AND filters.id_filter_child IN(4,6)

If you want to select all products, then there is no reason to also include the filters. 如果要选择所有产品,则没有理由也包括过滤器。 One approach is to use an aggregation and having clause: 一种方法是使用聚合和having子句:

SELECT p.*
FROM products p JOIN
     filters f
     ON p.product_id = f.id_product
WHERE p.published = '1'
GROUP BY p.product_id
HAVING SUM(f.id_filter_child = '4') > 0 AND
       SUM(r.id_filter_child = '6') > 0

This returns product that have both filter 4 and filter 6. 这将返回同时具有过滤器4和过滤器6的产品。

Note that I also fixed the query to use proper, explicit join syntax. 请注意,我还修复了查询以使用正确的显式join语法。

I like this method for "set-within-sets" subqueries, because it is quite general. 我喜欢这种“组内设置”子查询的方法,因为它非常通用。 You can add another filter to the HAVING clause with a similar condition. 您可以向条件类似的HAVING子句添加另一个过滤器。 You could get everything with 4 but not 6 by changing the second > 0 to = 0 . 通过将第二个> 0更改为= 0可以得到4而不是6的所有内容。

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

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