首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一条sql语句~该怎么解决

2012-02-22 
求一条sql语句~~IDPostIDUserID110001002100010031000100141001100----------------------------怎么取出U

求一条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

21000100
41001100

热点排行