Mysql 多条件选择

Mysql 多条件选择,mysql,sql,join,where-clause,Mysql,Sql,Join,Where Clause,我需要帮助创建一个查询 表A 表B +----+------------+-------------+ | id | product_id | taxonomy_id | +----+------------+-------------+ | 10 | 13 | 5 | | 11 | 13 | 2 | | 12 | 14 | 3 | | 13 | 15 |

我需要帮助创建一个查询 表A

表B

+----+------------+-------------+
| id | product_id | taxonomy_id |
+----+------------+-------------+
| 10 |         13 |           5 |
| 11 |         13 |           2 |
| 12 |         14 |           3 |
| 13 |         15 |           2 |
| 14 |         16 |          15 |
| 14 |         16 |           5 |
| 14 |         16 |          19 |
| 14 |         16 |          21 |
| 14 |         16 |          18 |
+----+------------+-------------+
我的尝试

SELECT *
FROM A
LEFT JOIN B ON B.product_id = A.product_id 
WHERE IF(B.taxonomy_id IN ('5','15'), 
         IF(B.taxonomy_id IN ('2'), 1, 0), 0) = 1
GROUP BY A.product_id
我需要它将表A中的结果返回给我

B.taxonomy_id为5或15,B.taxonomy_id为2

结果将是本例->产品标识-13
我还需要得到一些结果选择count*…->return为1,而不是在WHERE子句中进行过滤;您需要在HAVING子句中执行此条件筛选。您可以避免左联接,因为产品应该具有它们的分类2和5或15:

SELECT a.product_id 
FROM tablea a 
JOIN tableb b on b.product_id = a.product_id 
GROUP BY a.product_id 
HAVING SUM(b.taxonomy_id IN (5,15)) 
       AND SUM(b.taxonomy_id = 2)
结果


表没有id列作为唯一主键是正常的吗

不管怎样,这是我遇到的,告诉我它是否有效:

选择表\u name a.product\u id 来自表a 表\u nameA.product\u id=表\u nameB.product\u id上的左联接表\u nameB 其中,taxonomy_id=2,表_nameA.product_id位于 选择表\u name a.product\u id 来自表a 表\u nameA.product\u id=表\u nameB.product\u id上的左联接表\u nameB 其中分类法id=5或分类法id=15 按表\u nameA.product\u id、分类法\u id分组 结果是:

| product_id |
|------------|
|         13 |

| Quantity |
|----------|
|        1 |
关于计数查询,它完全相同

选择counttable\u nameA.product\u id作为数量 来自表a 表\u nameA.product\u id=表\u nameB.product\u id上的左联接表\u nameB 其中,taxonomy_id=2,表_nameA.product_id位于 选择表\u name a.product\u id 来自表a 表\u nameA.product\u id=表\u nameB.product\u id上的左联接表\u nameB 其中分类法id=5或分类法id=15 按表\u nameA.product\u id、分类法\u id分组 结果是:

| product_id |
|------------|
|         13 |

| Quantity |
|----------|
|        1 |
您可以使用exists:

如果您只是想要产品标识,那么我建议您使用聚合:

select b.product_id
from b
where b.taxonomy_id in (2, 5, 15)
group by b.product_id
having sum( b.taxonomy_id in (5, 15) ) > 0 and
       sum( b.taxonomy_id in (2) ) > 0 ;

您可以使用join with having子句或intersect函数和2个查询来获得输出

十三,

在你的senario。 加入have子句:

select a.product_id from a inner join b on a.product_id = b.product_id group by a.product_id having (SUM (b.taxonomy_id IN (5,15)) and SUM (b.taxonomy_id in (2)));
与2个查询相交:

select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id = 2))
INTERSECT
select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id in (5,15)));
对于计数,使用类似于此的东西,它将返回

一,

作为输出:

select COUNT(*) from (select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id = 2))
INTERSECT
select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id in (5,15)))) I;

你期望的结果是什么。。如果没有主键/唯一键,您或多或少会将InnoDB的多线程性能降级到MyISAM,MyISAM会锁定..您当前的where子句只会生成分类id不在[5,15,2]中的结果,因为一旦在“5”、“15”中的B.taxonomy\u id中变为true,就会直接转到“2”中的B.taxonomy\u id,这将产生false,从而导致0。我建议在'2','5',15中将其缩短为B.taxonomy\u id。问题是当我想获取->选择COUNTa.product。。。回报是2@user3061527你为什么要做选择计数?根据您最初的问题陈述,这不是必需的。请进一步解释,因为您似乎希望执行与原始查询完全不同的操作。我希望原始查询以及查询能够获得count@user3061527以表格形式添加预期结果,就像在问题中添加表格数据一样。然后只会显示一些清晰的信息。@user3061527除非使用子查询和窗口功能,否则无法在同一查询中获取计数。但好的是,您可以访问mysqli对象的num_rows成员以间接获取计数,而主查询将在单独的行中提供所有产品的id。
select COUNT(*) from (select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id = 2))
INTERSECT
select a.product_id from a where (a.product_id IN (select product_id from b where b.taxonomy_id in (5,15)))) I;