基于两个可互换字段的 SQL 排序表 [英] SQL Sorting table based on two interchangeable fields

查看:37
本文介绍了基于两个可互换字段的 SQL 排序表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按进行通信的顺序对具有 3 列(时间、来源、收件人)的表格进行排序.如果来源和收件人正在交谈,那么它会按时间列出他们.目标是查看按时间排序的相似人之间发生的通信.示例如下:

I want to sort a table having 3 columns (time, source , recipient) by the order by which communication is being made. If the source and recipient are conversing together then it will list them by the time. The goal is to see the communication happening between similar people ordered by time.An example is as:

time|source|recipient

1   paul    amy
2   amy     paul
3   amy     paul
5   paul    jane
8   amy     paul
9   jane    paul
10  paul    amy
11  paul    jane

最终结果是这样的

1   paul    amy
2   amy     paul
3   amy     paul
8   amy     paul
10  paul    amy
5   paul    jane
9   jane    paul
11  paul    jane

推荐答案

你的问题有点含糊.我有根据的猜测是你想要这个:

Your question is a bit vague. My educated guess is you want this:

SELECT *
FROM   tbl
ORDER  BY (GREATEST(source, recipient), LEAST(source, recipient), "time";

关于GREATEST<的手册/code> 和 LEAST.

这篇关于基于两个可互换字段的 SQL 排序表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆