未知的列在哪里......它的定义就在那里!

时间:2010-07-20 12:38:28

标签: sql mysql

我正在使用此查询

SELECT DISTINCT pr.competitorID AS compID, pr.age, CONCAT(pr.firstname, ' ', pr.lastname)AS name
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=1 AND participation_reports.competitorID=compID LIMIT 1) AS '100m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=25 AND participation_reports.competitorID=compID LIMIT 1) AS '200m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=37 AND participation_reports.competitorID=compID LIMIT 1) AS '400m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=49 AND participation_reports.competitorID=compID LIMIT 1) AS '800m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=61 AND participation_reports.competitorID=compID LIMIT 1) AS '1500m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=67 AND participation_reports.competitorID=compID LIMIT 1) AS '3000m'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=69 AND participation_reports.competitorID=compID LIMIT 1) AS 'Javelin'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=81 AND participation_reports.competitorID=compID LIMIT 1) AS 'Shot Put'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=93 AND participation_reports.competitorID=compID LIMIT 1) AS 'Discus'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=105 AND participation_reports.competitorID=compID LIMIT 1) AS 'High Jump'
, (SELECT  participation_reports.points FROM participation_reports WHERE participation_reports.heatEventID=117 AND participation_reports.competitorID=compID LIMIT 1) AS 'Long Jump'
, SUM(pr.points) AS total
FROM participation_reports AS pr
JOIN event_data ON event_data.eventID=pr.heatEventID
WHERE event_data.minAge <= pr.age AND event_data.maxAge >= pr.age AND sex = 'F' AND total > 0 LIMIT 30

但是我得到了这个错误:'where子句'中的'undefined column“total”'

<小时/> 更新:
欢呼伙计们,
任何人都在意解释有和哪里之间的区别(也有别名和定义)

4 个答案:

答案 0 :(得分:2)

它没有被定义,它是别名的,你不能这样做...尝试HAVING子句:

FROM participation_reports AS pr 
JOIN event_data ON event_data.eventID=pr.heatEventID 
WHERE event_data.minAge <= pr.age AND event_data.maxAge >= pr.age AND sex = 'F'
HAVING SUM(pr.points) > 0 
LIMIT 30 

答案 1 :(得分:2)

您不能在where子句中使用列别名。你必须说SUM(pr.points)&gt; 0

不幸的是,你也做不到。你必须说HAVING sum(pr.points) > 0

答案 2 :(得分:0)

您需要查看participation_reports表以确保它在那里。你的WHERE子句有它,但它所引用的表可能没有。检查一下。

答案 3 :(得分:0)

你被SQL的大脑破坏性语法所困扰。考虑SELECTFROMJOINWHERE作为函数。那么语义可以写成:

SELECT(
  WHERE(
    FROM(JOIN(R1, R2, COND))
  , COND
  )
, COLS
);

如您所见,您的total列名称不在WHERE子句的范围内。