日期:2014-05-19  浏览次数:20443 次

求一条sql语句~~
ID   PostID     UserID
1       1000         100
2       1000         100  
3       1000         1001
4       1001         100
----------------------------
怎么取出UserID=100的POSTID不一样的数据。
也就是得出的结果应该是
4       1001         100
2       1000         100

------解决方案--------------------
select max(id),postid,id from tab group by postid,id

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

最新版本:20070212

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
------解决方案--------------------

select * from T as tmp
where tmp.UserID= '100 ' and
(select count(*) from T where UserID= '100 ' and PostID=tmp.PostID)=1
------解决方案--------------------
--這樣?


create table T(ID int, PostID varchar(10), UserID varchar(10))
insert T select 1, '1000 ', '100 '
union all select 2, '1000 ', '100 '
union all select 3, '1000 ' , '1001 '
union all select 4, '1001 ' , '100 '

select ID=max(ID),PostID,UserID= '100 '
from T as tmp
where tmp.UserID= '100 '
group by PostID

--result
ID PostID UserID
----------- ---------- ------
2 1000 100
4 1001 100

(2 row(s) affected)

------解决方案--------------------
select max(id),postid,UserID
from tab
where UserID=100
group by postid,UserID

------解决方案--------------------
select max(id),PostID,UserID from tbmp
where UserID=100 group by PostID,UserID
------解决方案--------------------
借用ls的环境:)
create table T(ID int, PostID varchar(10), UserID varchar(10))
insert T select 1, '1000 ', '100 '
union all select 2, '1000 ', '100 '
union all select 3, '1000 ' , '1001 '
union all select 4, '1001 ' , '100 '


select max(id) ID,postid,UserID
from T
where UserID=100
group by postid,UserID


------------------------
ID postid UserID

2 1000 100
4 1001 100