求解 哪里不对了

shunzi403566 2011-08-01 08:15:12
题目:把EXKCode 相同, XShowtimes不相同的记录都选出来。
create table aaa
(
id int primary key,
exkcode varchar(10),
xshowtimes varchar(4)
)
insert into aaa values(1,'000654101','k1');
insert into aaa values(2,'000004130','k1');
insert into aaa values(3,'000654130','k1');
insert into aaa values(4,'000004130','k2');
insert into aaa values(5,'000604130','k1');
insert into aaa values(6,'000014102','k1');

Select *
From aaa a
Where id in(select id from aaa b where b.exkcode=a.exkcode and a.xshowtimes !=b.xshowtimes)
...全文
68 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlj768 2011-08-01
  • 打赏
  • 举报
回复
Select *
From aaa a
Where exists(select * from aaa where exkcode=a.exkcode and xshowtimes!=a.xshowtimes)

为什么要用select 1难明白
wlj768 2011-08-01
  • 打赏
  • 举报
回复

select a.* from aaa a inner join aaa b on a.exkcode = b.exkcode and a.xshowtimes <>b.xshowtimes
我用这个方法
--小F-- 2011-08-01
  • 打赏
  • 举报
回复
exists表示存在 只要存在就满足条件了
-晴天 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 shunzi403566 的回复:]
现在 我想知道 为什么 把id in换成exists 就能有结果了呢
[/Quote]
你语句的逻辑不对.
shunzi403566 2011-08-01
  • 打赏
  • 举报
回复
现在 我想知道 为什么 把id in换成exists 就能有结果了呢
ForFumm 2011-08-01
  • 打赏
  • 举报
回复
1表示非空
shunzi403566 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qianjin036a 的回复:]

SQL code
create table aaa
(
id int primary key,
exkcode varchar(10),
xshowtimes varchar(4)
)
insert into aaa values(1,'000654101','k1');
insert into aaa values(2,'000004130','k1');
insert into aaa……
[/Quote]

select 1 什么意思?
-晴天 2011-08-01
  • 打赏
  • 举报
回复
create table aaa
(
id int primary key,
exkcode varchar(10),
xshowtimes varchar(4)
)
insert into aaa values(1,'000654101','k1');
insert into aaa values(2,'000004130','k1');
insert into aaa values(3,'000654130','k1');
insert into aaa values(4,'000004130','k2');
insert into aaa values(5,'000604130','k1');
insert into aaa values(6,'000014102','k1');

Select *
From aaa a
Where exists(select 1 from aaa where exkcode=a.exkcode and xshowtimes!=a.xshowtimes)
/*id exkcode xshowtimes
----------- ---------- ----------
2 000004130 k1
4 000004130 k2

(2 行受影响)

*/
go
drop table aaa

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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