注册 登录
编程论坛 SQL Server论坛

急!!遇到问题,查询方面

古德里安 发布于 2013-08-27 21:57, 522 次点击
我有一个表A ,有字段id1,id2,id3,有若干条记录如
                     1   null   2
                     1   null   3
                     1   null   4
                     1      4   5
                     1      4   6
                     2   null   7
                     2   null   8
                     2   null   9
我想查询到id2的值等于4和与他id1值相同的那几条记录,就是前5条记录。这个查询要怎么写?
5 回复
#2
古德里安2013-08-27 22:15
是我没有描述清楚么怎么没有人啊
#3
Aviva_Wang2013-08-28 08:13
select * from a where id1= 1 or id2=4
select * from a where id1 =(select distinct id1 from a where id1=4)
#4
3037709572013-08-28 10:54
select * from [A] where id2=4 or id1 in (select distinct id1 from [A] where id2=4)
#5
古德里安2013-08-28 21:29
学习了,必须要子查询么?
#6
jxyga1112013-08-31 09:20
GROUP BY id1,id2 HAVING  id2 = 4,
1