开发者社区> 问答> 正文

sqlserver的查询语句,求解 - sqlserver报错

select a.id,
a.gamePassport,
a.name,
a.money,
a.chips,
(select count(b.id) from Extension as b where b.aId = a.userId) as num,
(select c.type from PersonnelType as c where c.id = a.typeId) as type,
a.userId from Apply as a where 1=1 and num > 5

这个sql语句...会报错列明num无效、我去掉最后的and num > 5,返回的是正确的。

难道sqlserver的子查询不能当做查询条件吗?

展开
收起
montos 2020-05-31 21:16:28 496 0
1 条回答
写回答
取消 提交回答
  • 你第一次查询的时候将子查询定义字段为num,但是在后面不能引用它,表apply并没有这个字段,所以不能解析

    例如

    select 1 as num from dual where num=1;

    error

    但是

    select * from (select 1 as num from dual )  a where num=1;

    正常运行,这时候你已经定义了一个新的表结构,包含了num字段,就可以解析了

    不知道讲的清楚不

    ######恩。讲的很清楚,我试了下。。ok了。。多谢。######你把查询的num用你上面的查询语句替换试试
    2020-05-31 21:16:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PolarDB NL2SQL: 帮助您写出准确、优化的SQL 立即下载
MySQL表和索引优化实战 立即下载
低代码开发师(初级)实战教程 立即下载