1. SELECT
2. (SELECT SUM(count) FROM _ERP_재고 as jego WHERE jego.goodsno=link.goodsno) as tt,
3. (SELECT catnm FROM gd_category WHERE category=LEFT(link.category, 3)) as cate1,
4. (SELECT catnm FROM gd_category WHERE category=LEFT(link.category, 6)) as cate2
5. FROM gd_goods as goods, gd_goods_link as link, _ERP_재고 as jego
6. WHERE link.goodsno=goods.goodsno
7. AND jego.goodsno=link.goodsno
8. AND (SELECT SUM(count) FROM _ERP_재고 as jego WHERE jego.goodsno=link.goodsno) > 0
9. AND link.category LIKE '009%' GROUP BY goods.goodsno
我提出了不恰当的查询,因为第2行与第8行相同
如果我用tt
(第2行的别名)替换第8行中的子选择,则会发生错误
请让我知道如何摆脱第二次调用子查询。
答案 0 :(得分:1)
您不能在WHERE
子句中使用别名。相反,您必须使用HAVING
。
1. SELECT
2. (SELECT SUM(count) FROM _ERP_재고 as jego WHERE jego.goodsno=link.goodsno) as tt,
3. (SELECT catnm FROM gd_category WHERE category=LEFT(link.category, 3)) as cate1,
4. (SELECT catnm FROM gd_category WHERE category=LEFT(link.category, 6)) as cate2
5. FROM gd_goods as goods, gd_goods_link as link, _ERP_재고 as jego
6. WHERE link.goodsno=goods.goodsno
7. AND jego.goodsno=link.goodsno
8. AND link.category LIKE '009%' GROUP BY goods.goodsno
9. HAVING tt > 0