我有以下mysql查询,
SELECT COUNT(*) as pg_total
FROM products p , prod_sub s
LEFT JOIN
(SELECT io.uni_id, count(io.uni_id) AS popular
FROM items_ord io GROUP BY io.uni_id ) as z
ON z.uni_id = p.uni_id
WHERE p_stock != 9
AND p.cat_id = 'ct_54a2e9fed3689'
AND p.uni_id = s.uni_id AND s.sub_id IN ('sb_54a2ea814a64d')
GROUP BY p.uni_id
ORDER BY z.popular DESC
但是这个查询工作正常,
SELECT COUNT(*) as pg_total
FROM products p
LEFT JOIN
(SELECT io.uni_id, count(io.uni_id) AS popular
FROM items_ord io GROUP BY io.uni_id ) z
ON z.uni_id = p.uni_id
WHERE p_stock != 9
AND p.cat_id = 'ct_54a2e9fed3689'
ORDER BY z.popular DESC
此查询抛出Unknown column 'p.uni_id' in 'on clause'
。我找不到问题。
答案 0 :(得分:0)
您错过了as
FROM items_ord io GROUP BY io.uni_id ) z
SELECT COUNT(*) as pg_total
FROM products p
LEFT JOIN
(SELECT io.uni_id, count(io.uni_id) AS popular
FROM items_ord io GROUP BY io.uni_id ) as z
ON z.uni_id = p.uni_id
WHERE p_stock != 9
AND p.cat_id = 'ct_54a2e9fed3689'
ORDER BY z.popular DESC
答案 1 :(得分:0)
运行此代码并复制输出并在sql
中过去并运行,您将获得哪个列未知
$sql = "SELECT COUNT(*) as pg_total
FROM products p , prod_sub s
LEFT JOIN
(SELECT io.uni_id, count(io.uni_id) AS popular
FROM items_ord io GROUP BY io.uni_id ) as z
ON z.uni_id = p.uni_id
WHERE p_stock != 9
AND p.cat_id = 'ct_54a2e9fed3689'
AND p.uni_id = s.uni_id AND s.sub_id IN ('sb_54a2ea814a64d')
GROUP BY p.uni_id
ORDER BY z.popular DESC";
echo $sql; exit;