mysql的结果是逗号分隔的where子句

kqqjbcuj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(311)

这种方法不需要我的where条件,但只要我搜索一种特定的口味,我就只能得到那种口味,但我想得到属于某个产品的所有口味。
我有两张table(简体):
产品

| id | title          |  
| 1  | exampleProduct |

口味

| taste_id | product_id | taste_name |  
|        1 |          1 | cherry     |  
|        2 |          1 | orange     |

我希望我的结果是:

| title          | tastes         |  
| exampleProduct | cherry, orange |

我已经尝试过此sql命令,但似乎不起作用:

SELECT products.title, GROUP_CONCAT(tastes.taste_name) as tastes   
FROM products 
JOIN tastes on products.id = tastes.product_id 
WHERE tastes.taste_name = "cherry" 
GROUP BY products.id
368yc8dk

368yc8dk1#

我猜你想要所有的口味,而“樱桃”是其中之一。
如果是这样,则需要在聚合后进行比较:

SELECT p.title, GROUP_CONCAT(t.taste_name) as tastes   
FROM products p JOIN
     tastes t
     ON p.id = t.product_id 
GROUP BY p.id
HAVING SUM(t.taste_name = 'cherry') > 0;

相关问题