在mysql中如何添加第三个条件?

时间:2018-04-20 19:19:53

标签: mysql sql database

使用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"它工作得很好,但我确实需要输出XYRange超过100。

你认为这是什么问题?

提前致谢

3 个答案:

答案 0 :(得分: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中的表达式。

答案 1 :(得分: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;

刚刚更新了最后一行,休息一切都一样。

答案 2 :(得分:0)

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

where ABS(stormtrooper_unit.Location_X - stormtrooper_unit.Location_Y)> 100