作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的查询如下:
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
表:
+--------------------+--------------+------+-----+---------+----------------+
| 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 | |
+--------------------+--------------+------+-----+---------+----------------+
如果需要,将发布其他表格。只是想不淹没帖子。谢谢!
编辑在 ct_product
中,我得到了如下产品(仅作为示例):
id
1
2
3
在 ct_product_attribute
中,每个产品可以有多个属性-attr.value 对。有些对是相同的。(将只显示我需要的列)
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
我从请求中得到的值是:
attribute_id=1
attribute_value_id=1
attribute_id=2
attribute_value_id=1
现在我只需要检索带有 id=1
的产品。如果我使用 OR,它会检索产品 id=1
和 id=2
。不确定现在是否变得更清楚了。
最佳答案
我很确定这些应该是 OR,因为您不能同时拥有所有这些 ID。考虑到这一点,您应该能够使用 IN。
WHERE cpa.attribute_id IN (10,2,7,9,8)
AND cpa.attribute_value_id IN (36,5,31,28,25)
关于mysql - 如何用这么多 AND 组织我的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53407109/
我是一名优秀的程序员,十分优秀!