2015-09-25 72 views
1

我有两个表。一个是product_details,另一个是size。 每个产品有多种尺寸。
我必须为每个产品设定尺寸(如100,125,150)。
但我已经在product_details表中将大小记录保存为JSON值(如[“1”,“2”,“8”])。
当我尝试使用IN子句得到记录&子查询,我无法获得记录。 检查此链接:click here sqlfiddleMysql子查询IN子句通过JSON值查找

SELECT po_det.po_det_size as json_str, 
REPLACE(REPLACE(REPLACE(po_det.po_det_size, '[',''),']',''),'"','')) str, 
(SELECT GROUP_CONCAT(size.size_name) 
FROM tbl_product_size size 
WHERE size.size_id IN (str) 
) AS sizes 
FROM tbl_purchase_order_details po_det 

请帮我弄的记录。

回答

2

对此,您不能使用in。您可以使用find_in_set()

where find_in_set(size.size_id, str) > 0 

我不认为你可以在子查询中使用列别名,所以这可能是另一个问题:

where find_in_set(size.size_id, REPLACE(REPLACE(REPLACE(po_det.po_det_size, '[',''),']',''),'"',''))) > 0 
+0

嗨,它的工作。非常感谢您节省我的时间.. –