pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

選擇與我交談過的用戶并與他們最后一條消息,

Select users with I have talked and last message with them like whatsapp(選擇與我交談過的用戶并與他們最后一條消息,例如 whatsapp)
本文介紹了選擇與我交談過的用戶并與他們最后一條消息,例如 whatsapp的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我的數(shù)據(jù)庫中有這張表:

I have this table on my database:

sentBysentTo 是對(duì) User 表的 FK.

sentBy and sentTo are FK to User table.

在這張桌子上我有用戶之間的消息:

On this table I have messages between users:

sentBy |  sentTo  |     dateSent     |     body
-------+----------+------------------+-----------------
  1    |    2     | 11/21/2010 10:00 | Hey!
-------+----------+------------------+-----------------
  2    |    1     | 11/21/2010 10:50 | Hi!
-------+----------+------------------+-----------------
  1    |    2     | 11/21/2010 10:51 | msg body 1
-------+----------+------------------+-----------------
  2    |    1     | 11/21/2010 11:05 | msg body 2
-------+----------+------------------+-----------------
  1    |    3     | 11/21/2010 11:51 | msg body 3
-------+----------+------------------+-----------------
  3    |    1     | 11/21/2010 12:05 | msg body 4
-------+----------+------------------+-----------------
  1    |    3     | 11/21/2010 12:16 | msg body 5
-------+----------+------------------+-----------------
  4    |    1     | 11/21/2010 12:25 | msg body 6
-------+----------+------------------+-----------------

我需要知道與用戶 1 交談過的用戶以及與用戶 1 交談過的用戶.在本例中,用戶 2、3 和 4(注意,用戶 4 已向用戶 1 發(fā)送消息,但用戶 1 尚未發(fā)送任何消息).

I need to know the users with whom user 1 has talked and users that have talked with user 1. In this case, with users 2, 3 and 4 (note that user 4 has sent a message to user 1, but user 1 hasn't sent any message yet).

第二個(gè)問題是:我怎樣才能獲得每個(gè)用戶的最后一條消息?我正在詢問是否獲得發(fā)送給用戶的最新消息.

And the second question is: how can I get the last message with each user? I'm asking about to get the latest message sent to a user.

例如,如果我詢問用戶 1,則用戶 2 的最新消息是:msg body 2.用戶 3 的最新消息是 msg body 5.

For example, if I'm asking about user 1, the latest message with user 2 is: msg body 2. And the latest message with user 3 is msg body 5.

如何在一個(gè) SQL SELECT 語句中獲取該信息?或者我可能需要兩次選擇.

How can I get that info in one SQL SELECT statement? Or maybe I will need two selects.

我正在嘗試做一些類似 WhatsApp 的事情.您有一個(gè)聊天屏幕,其中列出了我與之交談過的用戶列表(我的第一個(gè)問題),以及與他們交談的最后一條消息(我的第二個(gè)問題).

I'm trying to do something like WhatsApp. Where you have a chats screen with a list of users with whom I have talked (my first question), and the last message with them (my second question).

也許我可以創(chuàng)建另一個(gè)名為 Conversation 的表,將 sentBysentTo 移動(dòng)到該表,并將最后一條帶有發(fā)送日期的消息移動(dòng)到它,但我認(rèn)為這不是一個(gè)好的設(shè)計(jì).

Maybe I can create another table named Conversation, move sentBy and sentTo to that table, and also last message with the date sent to it, but I think this can't be a good design.

我的兩個(gè)問題的結(jié)果是:

The result for my two question is this:

sentBy |  sentTo  |     dateSent     |     body
-------+----------+------------------+-----------------
  2    |    1     | 11/21/2010 11:05 | msg body 2
-------+----------+------------------+-----------------
  1    |    3     | 11/21/2010 12:16 | msg body 5
-------+----------+------------------+-----------------
  4    |    1     | 11/21/2010 12:25 | msg body 6
-------+----------+------------------+-----------------

推薦答案

以下查詢將為您提供用戶 1 的預(yù)期結(jié)果:

The following query will give you the expected results for user 1:

select m.* from messages m
join (
  select auser,withuser,max(datesent) datesent from (
    select sentby as auser,sentto as withuser,datesent from messages 
    union
    select sentto as auser,sentby as withuser,datesent from messages 
    ) as ud
  group by auser,withuser
  ) maxud 
  on (m.datesent=maxud.datesent and maxud.auser in (m.sentBy,m.sentTo))
where auser=1

不用說,您可以更改 where 子句中的條件,以便為任何用戶獲得類似的結(jié)果.

Needless to say, you can change the condition in the where clause to get similar results for any user.

但是,我的方法是創(chuàng)建一個(gè)視圖,然后從中進(jìn)行選擇,如下所示:

However, my approach would be to create a view and later select from it, like so:

create view conversation_stuff as
select m.sentBy,m.sentTo,m.dateSent,m.body,maxud.auser,maxud.withuser
from messages m
join (
  select auser,withuser,max(datesent) datesent from (
    select sentby as auser,sentto as withuser,datesent from messages 
    union
    select sentto as auser,sentby as withuser,datesent from messages 
    ) as ud
  group by auser,withuser
  ) maxud 
  on (m.datesent=maxud.datesent and maxud.auser in (m.sentBy,m.sentTo))

select sentBy,sentTo,dateSent,body from conversation_stuff where auser=1;

我猜這對(duì)其他用途也很有用.

This could prove useful for other uses too, I guess.

EDIT:在任何地方將 user 更改為 auser,以便 sqlserver 停止抱怨以避免 []s...

EDIT: Changed user to auser everywhere, for sqlserver to stop complaining and to avoid []s...

這篇關(guān)于選擇與我交談過的用戶并與他們最后一條消息,例如 whatsapp的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應(yīng)該使用什么 SQL Server 數(shù)據(jù)類型來存儲(chǔ)字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數(shù)據(jù))
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉(zhuǎn)換為“2016-07-01 13:12:22小時(shí)格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應(yīng)該返回“1?什么時(shí)候不能投射為日期?)
主站蜘蛛池模板: 金现代信息产业股份有限公司--数字化解决方案供应商 | 釜溪印象网络 - Powered by Discuz!| 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 微型气象仪_气象传感器_防爆气象传感器-天合传感器大全 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 玻璃钢罐_玻璃钢储罐_盐酸罐厂家-河北华盛节能设备有限公司 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 轴承振动测量仪电箱-轴承测振动仪器-测试仪厂家-杭州居易电气 | 氧氮氢联合测定仪-联测仪-氧氮氢元素分析仪-江苏品彦光电 | 章丘丰源机械有限公司 - 三叶罗茨风机,罗茨鼓风机,罗茨风机 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 水热合成反应釜-防爆高压消解罐-西安常仪仪器设备有限公司 | TPU薄膜_TPU薄膜生产厂家_TPU热熔胶膜厂家定制_鑫亘环保科技(深圳)有限公司 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 北京签证代办_签证办理_商务签证_旅游签证_寰球签证网 | 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 24位ADC|8位MCU-芯易德科技有限公司| 氧化铁红厂家-淄博宗昂化工 | 干法制粒机_智能干法制粒机_张家港市开创机械制造有限公司 | 河南mpp电力管_mpp电力管生产厂家_mpp电力电缆保护管价格 - 河南晨翀实业 | 游动电流仪-流通式浊度分析仪-杰普仪器(上海)有限公司 | 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 水冷散热器_水冷电子散热器_大功率散热器_水冷板散热器厂家-河源市恒光辉散热器有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 收录网| 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 楼承板-开口楼承板-闭口楼承板-无锡海逵 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 |