当我们使用别名作为输出列,我们无法在Where条件中直接使用该列作判断条件.
例如下面的SQL语句:
select `id`,(select `name` from user where id=`user_id` )userName from home where userName="张三"
SQL Server 报错: "列名 userName 无效"
[]Err] 1054 - Unknown column 'userName' in 'where clause'
当然,写成
select * from( select `id`,(select `name` from user where id=`user_id` )userName from home)h where h.userName="张三"
解决方法:
SQL code复制代码
select * from( select `id`,(select `name` from user where id=`user_id` )userName from home)h where h.userName="张三"