3

是否可以从 MYSQL 中的两个别名的其余部分创建第三个别名?

这是我的查询:

SELECT DISTINCT
workcards.task, 
workcards.program, 
workcards.zone, 
workcards.CYLcomp, 
workcards.CYLint, 
workcards.task, 
workcards.CYLcomp + workcards.CYLint AS nextdue,
workcards.wcnumber,
(SELECT airtimes.total_cycles
       FROM airtimes 
       WHERE airtimes.nnumber='xxxxx'
       ORDER BY airtimes.total_cycles DESC LIMIT 1) AS current_cycle
FROM workcards
WHERE basis LIKE '%CYL%'
AND nnumber='xxxxx'
ORDER BY CASE nextdue
WHEN 0 THEN 1 ELSE -1 END ASC, nextdue ASC

我想要做的是创建第三个别名,这将是“nextdue”减去“current_cyle”别名的区别?就像是:

nextdue - current_cycle AS remainder?

对不起,如果noob或其他什么。

谢谢

4

1 回答 1

1

您不能将计算结果别名用于select部分其他计算(http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html)。但是你可以通过重建查询来获得你想要的东西,这样它将使用连接和分组:

SELECT DISTINCT 
  w.*, 
  w.CYLcomp + w.CYLint AS nextdue, 
  MAX(a.total_cycles) AS current_cycle
  w.CYLcomp + w.CYLint - MAX(a.total_cycles) AS reminder
FROM workcards w
JOIN airtimes a ON a.nnumber = w.nnumber
WHERE w.basis LIKE '%CYL%' AND w.nnumber='xxxxx'
GROUP BY w.nnumber
ORDER BY CASE nextdue WHEN 0 THEN 1 ELSE -1 END ASC, nextdue ASC
于 2012-12-05T23:29:53.127 回答