获取唯一用户ID以获取具有最新会话的用户列表

时间:2018-03-15 06:16:02

标签: php mysql

我希望获得与我进行最新对话的用户ID。

我不知道如何开始这个:

我的消息表结构如下:

id | msg_from_uid | msg_to_uid | msg_text | msg_date
----------------------------------------------------
1  | 8             | 3           | hello   | 2018-03-12 12:00:00
2  | 3             | 8           | hello   | 2018-03-12 12:11:00
3  | 8             | 5           | hello   | 2018-03-12 12:12:00
4  | 5             | 8           | hello   | 2018-03-12 12:13:00
5  | 8             | 7           | hello   | 2018-03-12 12:14:00
6  | 7             | 8           | hello   | 2018-03-12 12:15:00

假设我的用户ID是8,那么我如何在最新的会话顺序中获得不同的ids 3,5,7?

我只想要唯一的用户ID。这样我就可以在左侧边栏栏中列出它们,看看我与谁进行了最新的对话。

我的预期输出:

最新会话日期顺序中的用户ID:

user_id
----
7
5
3

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

一种选择是使用最小/最大的技巧来查找每个对话的最新记录。这在标有namespace MyProject.MyValueConverters { public class ArrayToStackLayoutConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var strArray = (string[])value; var layout = new StackLayout() { Orientation = StackOrientation.Horizontal }; foreach (var item in strArray) { var label = new Label() { Text = item }; var frame = new Frame() { Content = label, HasShadow = false, Padding = 4, OutlineColor = Color.Black, CornerRadius = 2 }; layout.Children.Add(frame); } return layout; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } } 的子查询中完成。然后,我们可以将原始表连接到此子查询以获取完整记录。最后,为了获得您想要的交易对手ID,我们可以选择匹配8(来源)的用户ID。

label.text = dropdownlist.selecteditem.text;

https://github.com/angular/zone.js/pull/795

enter image description here

编辑:如果您想要与最新会话对应的交易对手的用户ID,则实际上可能不需要加入。但是,我上面提到的查询可能对您有用,例如,如果您还想从每对用户之间的最新对话中获取文本。

答案 1 :(得分:1)

希望以下解决方案可以帮助你。

select * from ( SELECT distinct(msg_from_uid), msg_date FROM message where msg_to_uid = 8 UNION SELECT distinct(msg_to_uid), msg_date FROM message where msg_from_uid = 8 ) a group by msg_from_uid order by msg_date desc

输出

msg_from_uid msg_date 
3            2018-03-12 12:15:00
5            2018-03-12 12:13:00
7            2018-03-12 12:11:00

答案 2 :(得分:0)

您可以使用msg_date在DESC中订购查询来获取最新的会话。

select * from msg where msg_from_uid = 8 group by msg_to_uid order by msg_date DESC 

这样您将获得唯一的user_ids和最新消息。

相关问题