不能在 WHERE 子句中使用来自 SELECT 子句的别名
Cannot use alias from SELECT clause in the WHERE clause
使用stormtrooper_java;
SELECT imperial_battlegroup.BGID, imperial_battlegroup.Designation, imperial_battlegroup.HQ_LocationX, imperial_battlegroup.HQ_LocationY,
stormtrooper_unit.STUID, stormtrooper_unit.UnitCmd, stormtrooper_unit.UnitType, stormtrooper_unit.Location_X, stormtrooper_unit.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup inner join
stormtrooper_unit
on imperial_battlegroup.BGID = stormtrooper_unit.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry' AND
XYRange > 100;
当我在没有 XYRange > 100
的情况下执行文件时,它工作得很好,但我确实需要查询中的过滤逻辑。
如何调整我的查询以根据此计算条件过滤结果?
试试这个:
SELECT imperial_battlegroup.BGID, imperial_battlegroup.Designation,
imperial_battlegroup.HQ_LocationX, imperial_battlegroup.HQ_LocationY,
stormtrooper_unit.STUID, stormtrooper_unit.UnitCmd,
stormtrooper_unit.UnitType, stormtrooper_unit.Location_X,
stormtrooper_unit.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup inner join
stormtrooper_unit
on imperial_battlegroup.BGID = stormtrooper_unit.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry' AND
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) > 100;
刚刚更新了最后一行,其余一切都一样。
SQL 不允许您在 WHERE
子句中使用列别名。它确实扩展了 HAVING
,因此一种解决方案是:
SELECT bg.BGID, bg.Designation, imperial_battlegroup.HQ_LocationX, bg.HQ_LocationY,
u.STUID, u.UnitCmd, u.UnitType, u.Location_X, u.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup bg inner join
stormtrooper_unit
on bg.BGID = u.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry'
HAVING XYRange > 100;
另一种选择是重复WHERE
中的表达式。
禁止在where子句中使用别名。所以使用函数本身
where ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y)> 100
使用stormtrooper_java;
SELECT imperial_battlegroup.BGID, imperial_battlegroup.Designation, imperial_battlegroup.HQ_LocationX, imperial_battlegroup.HQ_LocationY,
stormtrooper_unit.STUID, stormtrooper_unit.UnitCmd, stormtrooper_unit.UnitType, stormtrooper_unit.Location_X, stormtrooper_unit.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup inner join
stormtrooper_unit
on imperial_battlegroup.BGID = stormtrooper_unit.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry' AND
XYRange > 100;
当我在没有 XYRange > 100
的情况下执行文件时,它工作得很好,但我确实需要查询中的过滤逻辑。
如何调整我的查询以根据此计算条件过滤结果?
试试这个:
SELECT imperial_battlegroup.BGID, imperial_battlegroup.Designation,
imperial_battlegroup.HQ_LocationX, imperial_battlegroup.HQ_LocationY,
stormtrooper_unit.STUID, stormtrooper_unit.UnitCmd,
stormtrooper_unit.UnitType, stormtrooper_unit.Location_X,
stormtrooper_unit.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup inner join
stormtrooper_unit
on imperial_battlegroup.BGID = stormtrooper_unit.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry' AND
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) > 100;
刚刚更新了最后一行,其余一切都一样。
SQL 不允许您在 WHERE
子句中使用列别名。它确实扩展了 HAVING
,因此一种解决方案是:
SELECT bg.BGID, bg.Designation, imperial_battlegroup.HQ_LocationX, bg.HQ_LocationY,
u.STUID, u.UnitCmd, u.UnitType, u.Location_X, u.Location_Y,
ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y) AS XYRange
from imperial_battlegroup bg inner join
stormtrooper_unit
on bg.BGID = u.UnitCmd
WHERE Designation = 'Battle Group I' and UnitType = 'Aslt Infantry'
HAVING XYRange > 100;
另一种选择是重复WHERE
中的表达式。
禁止在where子句中使用别名。所以使用函数本身
where ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y)> 100