这个sql语句错在哪了?

xingxingxiangrong 2009-01-17 01:21:12
ScoreId fen ScoreType ScoreUser
3 100 考核1 张三
4 99 考核1 张三
5 97 考核1 张三
6 96 考核1 张三
7 99 考核1 李四
8 95 考核1 张三
9 94 考核1 张三
10 99 考核2 张三
11 99 考核2 李四



select ScoreId from Score
where ScoreId in(select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo desc)
and Scoreid in(select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo) and ScoreType='考核1' and ScoreUser='张三'


我要查询的结果是
姓名为张三考核类型为考核1的2个最高记录和2个最低记录,但用我的查询语句确查不出来!

即查询结果为
3
4
8
9

最好能优话一下我的查询语句

...全文
129 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingxingxiangrong 2009-01-17
  • 打赏
  • 举报
回复
谢谢你们了
wojiaochenglong 2009-01-17
  • 打赏
  • 举报
回复
一开始和dawugui的想法一样,可是提醒楼主要注意有order by的情况下union的使用是有技巧的,要像dawugui那样写编译才能通过,可不要像我一样搞了半天也出不来
常见错误1:ORDER BY items must appear in the select list if the statement contains a UNION operator.
常见错误2:Incorrect syntax near the keyword 'union'.
受教.........
百年树人 2009-01-17
  • 打赏
  • 举报
回复
/**

ScoreId
-----------
3
4
8
9

(所影响的行数为 4 行)

**/
Dogfish 2009-01-17
  • 打赏
  • 举报
回复
select ScoreId from Score 
where
ScoreId in ( select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo desc)
or
Scoreid in (select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo)
百年树人 2009-01-17
  • 打赏
  • 举报
回复
select ScoreId from Score 
where
(
ScoreId in(select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo desc)
or
Scoreid in(select top 2 ScoreId from Score where ScoreType='考核1' and ScoreUser='张三' order by ScoreInfo)
)
and ScoreType='考核1' and ScoreUser='张三'
dawugui 2009-01-17
  • 打赏
  • 举报
回复
create table tb(ScoreId int, fen int, ScoreType varchar(10), ScoreUser varchar(10))
insert into tb values(3 , 100, '考核1' , '张三')
insert into tb values(4 , 99 , '考核1' , '张三')
insert into tb values(5 , 97 , '考核1' , '张三')
insert into tb values(6 , 96 , '考核1' , '张三')
insert into tb values(7 , 99 , '考核1' , '李四')
insert into tb values(8 , 95 , '考核1' , '张三')
insert into tb values(9 , 94 , '考核1' , '张三')
insert into tb values(10, 99 , '考核2' , '张三')
insert into tb values(11, 99 , '考核2' , '李四')
go

-- 姓名为张三考核类型为考核1的2个最低记录
select top 2 * from tb where ScoreUser = '张三' and ScoreType = '考核1' order by fen
/*
ScoreId fen ScoreType ScoreUser
----------- ----------- ---------- ----------
9 94 考核1 张三
8 95 考核1 张三

(所影响的行数为 2 行)
*/

-- 姓名为张三考核类型为考核1的2个最高记录
select top 2 * from tb where ScoreUser = '张三' and ScoreType = '考核1' order by fen desc
/*
ScoreId fen ScoreType ScoreUser
----------- ----------- ---------- ----------
3 100 考核1 张三
4 99 考核1 张三

(所影响的行数为 2 行)
*/

--合在一起显示
select top 100 percent * from (select top 2 * from tb where ScoreUser = '张三' and ScoreType = '考核1' order by fen) t
union all
select top 100 percent * from (select top 2 * from tb where ScoreUser = '张三' and ScoreType = '考核1' order by fen desc) t
order by scoreid
/*
ScoreId fen ScoreType ScoreUser
----------- ----------- ---------- ----------
3 100 考核1 张三
4 99 考核1 张三
8 95 考核1 张三
9 94 考核1 张三

(所影响的行数为 4 行)
*/

drop table tb

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧