首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用这么多的AND组织我的查询

如何用这么多的AND组织我的查询
EN

Stack Overflow用户
提问于 2018-11-21 07:26:18
回答 3查看 40关注 0票数 0

我的查询如下:

代码语言:javascript
复制
 SELECT SUM(ct_product_store_quantity.quantity) as quantity, `ct_product`.* 
FROM `ct_product`
LEFT JOIN `ct_productLang` ON `ct_product`.`id` = `ct_productLang`.`product_id`
LEFT JOIN `ct_product_store_quantity` ON `ct_product`.`id` = `ct_product_store_quantity`.`product_id` 
LEFT JOIN `ct_product_attribute` as cpa ON ct_product.id=cpa.product_id 
WHERE cpa.attribute_id=10 
AND cpa.attribute_value_id=36 
AND cpa.attribute_id=2 
AND cpa.attribute_value_id=5 
AND cpa.attribute_id=7 
AND cpa.attribute_value_id=31
AND cpa.attribute_id=9 
AND cpa.attribute_value_id=28
AND cpa.attribute_id=8 
AND cpa.attribute_value_id=25 
GROUP BY `ct_product`.`id`
HAVING quantity > 0 
ORDER BY `id` DESC

简单地说,每个AND条件都可以计算为true。如果我一个一个地处决他们,那就没问题了。但是,当我试图像我上面发布的那样执行它时,不会返回任何结果。我确信多个AND条件部分做得不对。ct_product_attribute表:

代码语言:javascript
复制
+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| product_id         | int(11)      | YES  | MUL | NULL    |                |
| attribute_set_id   | int(11)      | YES  | MUL | NULL    |                |
| attribute_id       | int(11)      | YES  | MUL | NULL    |                |
| attribute_value_id | int(11)      | YES  | MUL | NULL    |                |
| value              | varchar(255) | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

如有需要,将张贴其他表格。只是不想把柱子淹了。谢谢!

编辑 In ct_product,我得到的产品如下(例如):

代码语言:javascript
复制
id
 1
 2
 3

ct_product_attribute中,每个产品可以有多个属性-att.value对。其中一些对是相同的。(只显示我需要的列)

代码语言:javascript
复制
id product_id attribute_id attribute_value_id
1       1           1               1 
2       2           1               1
3       1           2               1
4       2           3               1
5       3           1               1
6       3           2               1

我从请求中得到的值是:

代码语言:javascript
复制
attribute_id=1
attribute_value_id=1
attribute_id=2
attribute_value_id=1

现在我只需要用id=1检索产品。如果我使用OR,它将同时检索产品id=1id=2。不确定现在是否变得更清楚了。

EN

回答 3

Stack Overflow用户

发布于 2018-11-21 07:33:03

我很确定这些应该是OR,因为您不可能同时拥有所有这些ID。考虑到这一点,您应该能够使用IN。

代码语言:javascript
复制
WHERE cpa.attribute_id IN (10,2,7,9,8) 
AND cpa.attribute_value_id IN (36,5,31,28,25)
票数 3
EN

Stack Overflow用户

发布于 2018-11-21 07:33:19

我真的不知道您想要完成什么,但是您应该/可以使用WHERE IN,正如每个人在注释中指出的,您正在寻找一个具有多个值的字段.

但是,关于和问题,您可以/应该使用IN,如

代码语言:javascript
复制
 SELECT SUM(ct_product_store_quantity.quantity) as quantity, `ct_product`.* 
FROM `ct_product`
LEFT JOIN `ct_productLang` ON `ct_product`.`id` = `ct_productLang`.`product_id`
LEFT JOIN `ct_product_store_quantity` ON `ct_product`.`id` = `ct_product_store_quantity`.`product_id` 
LEFT JOIN `ct_product_attribute` as cpa ON ct_product.id=cpa.product_id 
WHERE cpa.attribute_id IN (10, 2, 7, 9, 8)
AND cpa.attribute_value_id IN (36, 5, 31, 28, 25)
GROUP BY `ct_product`.`id`
HAVING quantity > 0 
ORDER BY `id` DESC
票数 1
EN

Stack Overflow用户

发布于 2018-11-21 07:36:40

您可以尝试使用(cpa.attribute_id,cpa.attribute_value_id) in ((10,36),(2,5),(7,31),(9,28),(8,25))

代码语言:javascript
复制
SELECT SUM(ct_product_store_quantity.quantity) as quantity, `ct_product`.* 
FROM `ct_product`
LEFT JOIN `ct_productLang` ON `ct_product`.`id` = `ct_productLang`.`product_id`
LEFT JOIN `ct_product_store_quantity` ON `ct_product`.`id` = `ct_product_store_quantity`.`product_id` 
LEFT JOIN `ct_product_attribute` as cpa ON ct_product.id=cpa.product_id 
WHERE (cpa.attribute_id,cpa.attribute_value_id) in ((10,36),(2,5),(7,31),(9,28),(8,25)) and `ct_product`.`id`=1
GROUP BY `ct_product`.`id`
HAVING quantity > 0 
ORDER BY `id` DESC
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53407109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档