请让我知道mysql查询:where子句中的别名

时间:2014-10-28 01:52:45

标签: mysql syntax alias

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行中的子选择,则会发生错误 请让我知道如何摆脱第二次调用子查询。

1 个答案:

答案 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