0

使用冲锋队_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时效果很好,但我确实需要查询中的过滤逻辑。

如何调整我的查询以按此计算条件过滤结果?

4

3 回答 3

1

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.

于 2018-04-20T19:32:35.493 回答
0

禁止在 where 子句中使用别名。所以使用函数本身

where ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y)> 100
于 2018-04-20T19:35:20.797 回答
0

尝试这个:

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;

刚刚更新了最后一行,其余的都是一样的。

于 2018-04-20T19:25:46.633 回答