读书人

求教一个关于子查询的有关问题~

发布时间: 2012-03-03 15:33:03 作者: rapoo

求教一个关于子查询的问题~~~~~
select 用户ID,记录类型 as 最后操作 from 记录表 where
记录时间 =(select top 1 用户ID,记录时间 from 记录表 where 用户ID= '1 'order by 记录时间 desc)

select 用户ID,记录类型 as 最后操作 from 记录表 where
记录时间 in (select top 1 用户ID,记录时间 from 记录表 where 用户ID= '1 'order by 记录时间 desc)

这2种写法都是报错:
服务器: 消息 116,级别 16,状态 1,行 1
当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。

但我看书上很多地方也是直接用的where=(子查询)之类的操作啊?

另外,select 用户ID,记录类型 as 最后操作 from 记录表 where
exists(select top 1 用户ID,记录时间 from 记录表 where 用用户ID= '1 'order by 记录时间 desc)

还是返回的所有的记录(我的想法是只返回时间=select top 1 用户ID,记录时间 from 记录表 where 用用户ID= '1 'order by 记录时间 desc中的那个记录的)


[解决办法]
--既然是与记录时间匹配,为什么在子查询数据数据列的列表中列出用户ID?多余
select 用户ID,记录类型 as 最后操作 from 记录表 where
记录时间 =(select top 1 记录时间 from 记录表 where 用户ID= '1 'order by 记录时间 desc)

--同上
select 用户ID,记录类型 as 最后操作 from 记录表 where
记录时间 in (select top 1 记录时间 from 记录表 where 用户ID= '1 'order by 记录时间 desc)

读书人网 >SQL Server

热点推荐