关于SQL中表的别名使⽤注意事项
1.在SQL语句中⼀旦指定了表别名,则 不能再使⽤"表名称.列名"格式, 只能使⽤" 表别名.列名"格式来引⽤列.
2.在SQL主句中使⽤⼦查询中的列时,必须为⼦查询指定⼀个表别名,然后以"表别名.列名"的格式来应⽤⼦查询中的列.当SQL主句的FROM⼦句仅为⼀个⼦查询时,必须为该⼦查询指定⼀个表别名,否则SQL语句⽆法执⾏.例如:
SELECT ID FROM (select e.ID from employee as e where (e.年度 = 2000) AND (e.⽉份 = 3))
提⽰: ')' 附近有语法错误。正确的应该为:
SELECT ID FROM (select e.ID from employee as e where (e.年度 = 2000) AND (e.⽉份 = 3)) AS X
3.⼦查询中使⽤了"表别名.列名"来引⽤列时,若此表别名在⼦查询和SQL主句中都不存在,则导致SQL语句⽆法执⾏.例如:
SELECT ID FROM (select e.ID from employee where (e.年度 = 2000) AND (e.⽉份 = 3)) AS X
提⽰: 列前缀 'e' 与查询中所⽤的表名或别名不匹配。
如果SQL主句中存在此表别名,则错误不会发⽣,但会导致⽆法预料的结果(通常导致该⼦查询返回结果为空).例如:
sql中select是什么意思SELECT e.* FROM (select * from sales as e where (e.年度 = 2006) and (e.⽉份 = 3)) AS e
WHERE (e.员⼯号 NOT IN (select e.员⼯号 from sales where (e.年度 = 2000) AND (e.⽉份 = 3)))
此SQL语句执⾏后,导致主句的WHERE⼦句的⼦查询返回空值,使WHERE⼦句结果为真,筛选实效.
正确的为:
SELECT e.* FROM (select * from sales as e where (e.年度 = 2006) and (e.⽉份 = 3)) AS e
WHERE (e.员⼯号 NOT IN (select e.员⼯号 from sales as e where (e.年度 = 2000) AND (e.⽉份 = 3)))
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论