我有以下
MySQL查询运行绝对正常:
SELECT a.id, a.event_name, c.name, a.reg_limit-e.places_occupied AS places_available, a.start_date
FROM nbs_events_detail AS a, nbs_events_venue_rel AS b, nbs_events_venue AS c,
(SELECT e.id, COUNT(d.event_id) AS places_occupied FROM nbs_events_detail AS e LEFT JOIN nbs_events_attendee AS d ON e.id=d.event_id GROUP BY e.id) AS e
WHERE a.id=b.event_id AND b.venue_id=c.id AND a.id=e.id AND a.event_status='A' AND a.start_date>=NOW()
ORDER BY a.start_date
但是,我正在尝试添加一个WHERE子句,以便过滤为减法创建的列中显示的结果:a.reg_limit-e.places_occupied AS places_available
到目前为止我所做的是添加如下的WHERE子句:
在哪里places_available> 0
但是,如果我尝试使用此指令,查询将失败,并且不会显示任何结果.报告的错误如下:#1054 – ‘where子句’中的未知列’places_available’
在a.reg_limit中我有数字,在e.places_occupied中我有子查询中COUNT生成的数字.
我错过了什么?
最佳答案 您在WHERE子句中的查询中定义的
can not use aliases.为了使您的查询有效,请在WHERE子句中使用条件a.reg_limit-e.places_occupied> 0.