所以我的 table 有:
column1, column2, column3 & datetime
SELECT * FROM mytable WHERE column1 = :column1
CONCAT column1 & column2 (column1NRcolumn2) to get a string to search for duplicates.
SELECT 1 column1_column2 WHERE datetime is highest
我想要的是合并两个单元格以创建一个新的临时列。 (列 1NR 列 2)
然后我想查找重复项,然后选择最新的 column1NRcolum2 WHERE datetime is highest/latest
我该如何管理?
最佳答案
您可以对计数 > 1 的串联列使用子查询,并加入您的表以获得最大日期时间
select t.my_concat, max(m.datetime)
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
如果您需要三个相关的 mytable 列,您需要在最大日期时间上进行连接
select m2.*
from mytable m2
inner join
(
select t.my_concat, max(m.datetime) max_datetime
from mytable m
inner join (
SELECT concat(column1,column2) my_concat
FROM mytable
group by concat(column1,column2)
having count(*)>1
) t
group by t.my_concat
) t2 on t2.my_concat = m2. concat(column1,column2)
and t.max_datetime = m2.datetime
关于mysql - 连接两列以查找重复项,然后选择最新日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415282/