[英]MYSQL query that can group by the highest datetime from two rows that might have the same value but flip flopped
I have a MYSQL 5.x database that I can't change the schema, read only access: 我有一个无法更改架构的MYSQL 5.x数据库,只读访问权限:
SELECT * FROM table;
--------------------+------------+------------+---------
stamp | src | dst | data
--------------------+------------+------------+---------
2017-12-06 11:08:39 | 2055551234 | 2059999999 | somedata
2017-12-04 16:04:14 | 2059999999 | 2055551234 | somedata
2017-12-04 14:28:21 | 2055551234 | 2059999999 | somedata
2017-12-04 14:26:24 | 8004441111 | 2562428766 | somedata
2017-12-04 14:06:22 | 2562428766 | 8004441111 | somedata
2017-12-04 13:15:52 | 8004441111 | 2562428766 | somedata
+---------------------+------------+------------+-------
I need all rows that service sets of two numbers, for example 2055551234 and 2055551234, each can be SRC or DST. 我需要为两个数字集服务的所有行,例如2055551234和2055551234,每个行都可以是SRC或DST。 I need the most recent row. 我需要最新行。
I need rows that contain both 2055551234 and 2055551234 but in either SRC or DST columns. 我需要同时包含2055551234和2055551234但在SRC或DST列中的行。 The values can be in either column and thus my problem. 值可以在任一列中,因此是我的问题。
Each of the matching SRC,DST sets has multiple rows, I need the row with the most recent datetime from each set. 每个匹配的SRC,DST集都有多行,我需要每组中具有最新日期时间的行。
I thought I had it outsmarted when I decided to try to create a hash with MD5(SRC+DST) which would also equal MD5(DST+SRC) but I got unreliable results. 当我决定尝试使用MD5(SRC + DST)创建一个哈希值(也等于MD5(DST + SRC))时,我以为它不明智,但结果却不可靠。
There can be anywhere from 1 to thousands of groups of rows that have matching sets. 可以有1到数千行具有匹配集的行组。
I need to be able to LIMIT the results so just iterating client side isn't going to work because of the unknown rows in each group. 我需要能够限制结果,因此由于每个组中的未知行,仅迭代客户端将无法工作。
I can't store any procedures, any iterating work will have to be done client side. 我不能存储任何程序,任何迭代工作都必须在客户端完成。
From the above data I would like: 根据以上数据,我想:
--------------------+------------+------------+---------
stamp | src | dst | data
--------------------+------------+------------+---------
2017-12-06 11:08:39 | 2055551234 | 2059999999 | somedata
2017-12-04 14:26:24 | 8004441111 | 2562428766 | somedata
+---------------------+------------+------------+-------
Thoughts? 思考?
Ah, now I think I got it. 嗯,现在我想我明白了。 You are not looking for two particular numbers; 您不是在寻找两个特定的数字。 you are looking for groups of rows that have the same number pair, no matter in what order. 您正在寻找具有相同数字对的行组,无论顺序如何。 So get the lesser and the greater value and group. 因此获得的价值和群体越来越小。
select *
from mytable
where (least(src, dst), greatest(src, dst), stamp) in
(
select least(src, dst), greatest(src, dst), max(stamp)
from mytable
group by least(src, dst), greatest(src, dst)
);
SQL fiddle: http://sqlfiddle.com/#!9/5bed58/1 SQL提琴: http ://sqlfiddle.com/#!9/ 5bed58/1
SELECT *
FROM mytbl AS t1
WHERE stamp IN (
SELECT MAX(stamp) AS maxStamp
FROM (
SELECT *, CONCAT(GREATEST(src, dst), '.', LEAST(src, dst)) AS gbval
FROM `mytbl` AS t2
) AS t3
GROUP BY t3.gbval
HAVING maxStamp = t1.stamp
)
Does this work? 这样行吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.