我有一张看起来像这样的 table
products_table
ID | code | product_variants_id | product_name | variants | variants_value
1 | 1| 123451 | beer cake | color | blue
2 | 1| 123451 | beer cake | temperature | hot
3 | 1| 123451 | beer cake | weight | 0.5
4 | 2| 123453 | ad wrap | color | green
5 | 2| 123453 | ad wrap | weight | 1
我运行了以下查询以获取具有相应变体的产品的唯一行。
SELECT xx.code, GROUP_CONCAT(concat(xx.variants,':',xx.variants_value))
AS variants_and_values, xx.product_name, xx.product_variants_id
FROM products_table xx
GROUP BY xx.product_variants_id, xx.product_name, xx.code
下表是上面查询的结果。现在我可以简单地遍历这个表并显示产品。
code | product_variants_id | product_name | variants_and_values
1 | 123451 | beer cake | color:blue,temperature:hot,weight:0.5
2 | 123453 | ad wrap | color:green,weight:1
现在真正的问题是,如果我要搜索上表并只显示变体为 hot
的那些产品,我该怎么做?
最佳答案
select * from t where variants like '%:hot' or variants like '%:hot,%'
关于MYSql 在连续列中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183507/