简体   繁体   English

可以从可能具有相同值但被翻转的两行中按最高日期时间分组的MYSQL查询

[英]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.

相关问题 如何将mySQL中具有相同值的行分组到php中? - How can I group rows with the same value from mySQL into php? 如何查询具有第v.2列之一相同值的行中具有最高列值的行 - How to query for rows that have highest column value among rows that have same value for one of the columns v.2 MySQL查询:选择组中具有最高值的记录 - MySQL query: select records with highest value in group MySQL查询返回的行比预期的多,我如何仅从每个组返回一列的最大值? - MySQL query returns more rows than intended, how do i only return the highest value of one column from each group? 如果两个或更多列具有相同的值,如何获取具有最高值的行 - How to get rows with highest value, if two or more Columns have same value PHP MYSQL需要对在某些列中具有相同值的行进行分组 - PHP MYSQL need to group rows that have same value in certain column 优化查询-如何从日期时间列中选择最高和最低行,按日期分组并返回数组 - Optimize query - How can I Select highest and lowest row, group by date from datetime column and return array MySQL GROUP_CONCAT并翻转两行 - MySQL GROUP_CONCAT and flip two rows in one 通过查询获取MySQL组以显示该组中具有最高值的行 - Getting a MySQL group by query to display the row in that group with the highest value 两组同查询Mysql - Two group in the same Query Mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM