我需要在子查询中执行子查询,导致'where子句'中出现“未知列't1.product_id'”。在我的例子中,它在第7行。如何解决这个问题?
SELECT *,product_id id,
(SELECT GROUP_CONCAT (value ORDER By `order` ASC SEPARATOR ', ')
FROM (
SELECT `order`,value
FROM slud_data
LEFT JOIN slud_types ON slud_types.type_id=slud_data.type_id
WHERE slud_data.product_id = t1.product_id
AND value!='' AND display=0
LIMIT 3
) tmp) text
FROM slud_products t1
WHERE
now() < DATE_ADD(date,INTERVAL +ttl DAY) AND activated=1
ORDER BY t1.date DESC
继续
答案 0 :(得分:5)
我发现在超过2选择深度的查询中使用变量更为舒适。
SELECT *,@product := product_id id,
(SELECT GROUP_CONCAT (value ORDER By `order` ASC SEPARATOR ', ')
FROM (
SELECT `order`,value
FROM slud_data
LEFT JOIN slud_types ON slud_types.type_id=slud_data.type_id
WHERE slud_data.product_id = @product
AND value!='' AND display=0
LIMIT 3
) tmp) text
FROM slud_products t1
WHERE
now() < DATE_ADD(date,INTERVAL +ttl DAY) AND activated=1
ORDER BY t1.date DESC
答案 1 :(得分:3)
使用派生表/内联视图和表别名:
SELECT product_id AS id,
GROUP_CONCAT (y.value ORDER BY y.`order`)
FROM slud_products t1
JOIN (SELECT sd.product_id,
sd.value,
sd.`order`
FROM SLUD_DATA sd
LEFT JOIN slud_types ON slud_types.type_id = slud_data.type_id
WHERE value! = ''
AND display = 0) y ON y.product_id = t1.product_id
AND y.order <= 3
WHERE now() < DATE_ADD(date,INTERVAL +ttl DAY)
AND activated = 1
GROUP BY product_id
ORDER BY t1.date DESC