示例:
产品
| PK(AI) | product_id | product_part |
| | 1 | bolt |
| | 1 | gear |
| | 2 | bolt |
| | 2 | steel |
然后我尝试选择product_id,其中product_part有(bolt 和齿轮)
我在尝试
`SELECT * FROM product WHERE product_part LIKE '%bolt%' AND product_part LIKE '%gear%'`
and result is nothing. should be result is product_id 1.
注意:它必须有2 不(OR)
答案 0 :(得分:1)
你必须使用这样的分组:
SELECT product_id
FROM product
WHERE product_part LIKE '%bolt%' OR product_part LIKE '%gear%'
GROUP BY product_id
HAVING COUNT(CASE WHEN product_part LIKE '%bolt%' THEN 1 END) > 0 AND
COUNT(CASE WHEN product_part LIKE '%gear%' THEN 1 END) > 0
通过这种方式,您可以选择与相关的product_id
值'%bolt%' - 和'%gear%' - 类似product_part
值。
答案 1 :(得分:0)
您可以在同一张桌子上执行INNER JOIN:
SELECT p1.product_id FROM product AS p1
INNER JOIN product AS p2
ON p1.product_id = p2.product_id
WHERE
p1.product_part LIKE '%bolt%' AND
p2.product_part LIKE '%gear%'