亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

私信分組并顯示最新一條記錄,請問SQL語句如何寫?

私信分組并顯示最新一條記錄,請問SQL語句如何寫?

吃雞游戲 2018-12-06 22:12:12
create table tbl_message ( id int primary key identity(1,1), --主鍵 senderId int not null, --發件人 receiverId int not null, --收件人 content nvarchar(500) not null, --私信內容 postTime datetime default(getdate()) not null --發送時間 ) insert into tbl_message(senderId,receiverId,content) values(1,2,'hi,how are you?') insert into tbl_message(senderId,receiverId,content) values(1,3,'hi,how are you?') insert into tbl_message(senderId,receiverId,content) values(1,4,'hi,how are you?') insert into tbl_message(senderId,receiverId,content) values(2,1,'fine,thank you! and you?') insert into tbl_message(senderId,receiverId,content) values(3,1,'not bad!') insert into tbl_message(senderId,receiverId,content) values(4,1,'a bad day!') insert into tbl_message(senderId,receiverId,content) values(1,2,'i am fine too!') ? 下面舉例子: id senderId receiverId content postTime ----------- ----------- ----------- ------------------------------------- 1 A B hi,how are you? 2014-06-30 2 A C hi,how are you? 2014-06-30 3 A D hi,how are you? 2014-06-30 4 B A fine,thank you! and you?   2014-06-31 5 C A not bad!   2014-06-31 6 D A a bad day! 2014-06-31 7 A B i am fine too! 2014-06-32 如上表中:A-B、A-C、A-D 分組,然后每組取最新一條記錄,結果如下三組 id senderId receiverId content postTime ----------- ----------- ----------- ------------------------------------- 5 C A not bad!   2014-06-31 6 D A a bad day! 2014-06-31 7 A B i am fine too! 2014-06-32
查看完整描述

12 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

senderId和receiverId是什么類型的?如果是整形比較簡單,如果是字符型,需要稍微處理一下。

查看完整回答
反對 回復 2019-01-07
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

A-B、A-C、A-D 分組是什么意思呢?沒有理解

查看完整回答
反對 回復 2019-01-07
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

看懂了,你的意思是senderId和receiverId中只要有A-B、A-C、A-D就分組是吧

就是互相發過信息的分為一組

查看完整回答
反對 回復 2019-01-07
?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

A-B、A-C、A-D?代表分組對象,比如A和B的聊天記錄為一組,A和C的聊天記錄為一組,A和D的聊天記錄為一組,然后取出每一組的一條最新聊天記錄。

查看完整回答
反對 回復 2019-01-07
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

@劉宏璽: 是的

查看完整回答
反對 回復 2019-01-07
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

@hillan: 什么數據庫呢?

查看完整回答
反對 回復 2019-01-07
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

@劉宏璽: sqlserver

查看完整回答
反對 回復 2019-01-07
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

@hillan: 我試試

查看完整回答
反對 回復 2019-01-07
?
呼如林

TA貢獻1798條經驗 獲得超3個贊

@hillan:?

select [id],[senderId],[receiverId],[content],[postTime] from [dbo].[Table_1] a
right join
(
select max([postTime]) time,
case
when [senderId] < [receiverId] then [senderId] + [receiverId]
else [receiverId] + [senderId]
end senderIdreceiverId
from [dbo].[Table_1]
group by
case
when [senderId] < [receiverId] then [senderId] + [receiverId]
else [receiverId] + [senderId]
end
) b
on a.[postTime] = b.time

查看完整回答
反對 回復 2019-01-07
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

@劉宏璽:?

select [id],[senderId],[receiverId],[content],[postTime] from [dbo].[tbl_message] a
right join 
(
select  max([postTime]) time,
case
when [senderId] < [receiverId]  then CAST([senderId] AS nvarchar(20)) + '|' + CAST([receiverId] AS nvarchar(20))
else CAST([receiverId] AS nvarchar(20)) + '|'+ CAST([senderId] AS nvarchar(20))
end senderIdreceiverId
from [dbo].[tbl_message]
group by 
case
when [senderId] < [receiverId]  then CAST([senderId] AS nvarchar(20)) + '|'+ CAST([receiverId] AS nvarchar(20))
else CAST([receiverId] AS nvarchar(20)) + '|'+ CAST([senderId] AS nvarchar(20))
end
) b
on a.[postTime] = b.time

老兄,我這個都實現了,但是的的postTime是不能相同的,我覺得這個postTime按道理是不能相同的吧!

你不采納倒是把原因說一下啊

查看完整回答
反對 回復 2019-01-07
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

這個 用MySql如何寫?。?/p>

查看完整回答
反對 回復 2019-01-07
  • 12 回答
  • 0 關注
  • 933 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號