如何在 SQL 的 where 子句中使用别名 [重复]
Posted
技术标签:
【中文标题】如何在 SQL 的 where 子句中使用别名 [重复]【英文标题】:how to use Alias Name in where clause in SQL [duplicate] 【发布时间】:2021-12-29 17:55:38 【问题描述】:首先,我在这里查看了大部分答案,但我无法将解决方案应用于我的案例。 它总是给我一个错误。
我需要在 where 子句中使用“FinalTotal”别名:
SELECT
p.ID,
p.TheName0,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID
and cls.TheDate BETWEEN @Date1 and @Date2
and att.TheStatus <> 'N'
) as CurrMost,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID and cls.TheDate< @Date1 and att.TheStatus <> 'N'
) as PrevMost,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate BETWEEN @Date1 and @Date2
) as CurrMadf,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate < @Date1
) as PrevMadf,
(SELECT CurrMost + PrevMost) as AllMost,
(SELECT CurrMadf + PrevMadf) as AllMadf,
(SELECT AllMost - AllMadf) AS FinalTotal
from tbl_parents p
WHERE FinalTotal <> 0
但我得到了错误:
#1054 - Unknown column 'FinalTotal' in 'field list'
【问题讨论】:
只需将WHERE FinalTotal <> 0
替换为WHERE AllMost - AllMadf <> 0
,并将(SELECT AllMost - AllMadf)
替换为AllMost - AllMadf
...
finalTotal column 在 WHERE 子句中不可用,因为(逻辑上)在创建列别名之前评估了 WHERE 子句。 mysql 的特殊技巧是使用 HAVING 子句。
【参考方案1】:
你应该使用子查询语法:
SELECT * FROM (
SELECT
p.ID,
p.TheName0,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID
and cls.TheDate BETWEEN @Date1 and @Date2
and att.TheStatus <> 'N'
) as CurrMost,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID and cls.TheDate< @Date1 and att.TheStatus <> 'N'
) as PrevMost,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate BETWEEN @Date1 and @Date2
) as CurrMadf,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate < @Date1
) as PrevMadf,
(SELECT CurrMost + PrevMost) as AllMost,
(SELECT CurrMadf + PrevMadf) as AllMadf,
(SELECT AllMost - AllMadf) AS FinalTotal
from tbl_parents p
) as tab
WHERE FinalTotal <> 0
【讨论】:
【参考方案2】:这真的没有意义,因为没有GROUP BY
但是改变这个
WHERE FinalTotal <> 0
到这里
HAVING FinalTotal <> 0
简化测试
create table test ( id int auto_increment primary key, a int, b int ); insert into test (a, b) values (1,1),(2,3); select a, b , a + b as ab , (select ab + 10) as ab10 from test having ab10 > 12;
a | b | ab | ab10 |
---|---|---|---|
2 | 3 | 5 | 15 |
db小提琴here
【讨论】:
以上是关于如何在 SQL 的 where 子句中使用别名 [重复]的主要内容,如果未能解决你的问题,请参考以下文章