2016-10-21 108 views
1

我有一个表,看起来像这样MySQL的搜索

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 

下表作为上述query.Now我可以简单地通过该表的运行和显示产品的结果而获得。

 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,只显示那些产品,我要怎么做呢?

回答

1
select * from t where variants like '%:hot' or variants like '%:hot,%' 
+0

不会是简单的写'%:热%'? –

+1

而文本是** hotio **?你应该忽略**热**作为前缀 –

+0

够公平,我的朋友。 –

1

试试这个

SELECT T.* 
FROM(
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 
) T 
WHERE T.variants_and_values LIKE '%:hot' OR T.variants_and_values LIKE '%:hot,' 
0
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 
WHERE (xx.variants = 'temperature' AND xx.variants_value = 'hot') 
GROUP BY xx.product_variants_id, xx.product_name, xx.code