MySQL选择不同 [英] Mysql select distinct
问题描述
我正在尝试选择mysql表中的重复行,这对我来说很好,但是问题在于,它不是让我选择该查询中的所有字段,而是让我选择我用作不同字段的字段名称, lemme编写查询以获得更好的了解
I am trying to select of the duplicate rows in mysql table it's working fine for me but the problem is that it is not letting me select all the fields in that query , just letting me select the field name i used as distinct , lemme write the query for better understading
mysql_query("SELECT DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
mysql_query("SELECT * , DISTINCT ticket_id FROM temp_tickets ORDER BY ticket_id")
第一个工作正常
现在,当我尝试选择所有字段时,我都会出现错误
now when i am trying to select all fields i am ending up with errors
我正在尝试选择最新的重复项,比如说ticket_id 127是ID为3、8、9的行的3次,因此我想选择一次,最新条目在这种情况下为9,这适用于票证ID的其余所有内容
i am trying to select the latest of the duplicates let say ticket_id 127 is 3 times on row id 7,8,9 so i want to select it once with the latest entry that would be 9 in this case and this applies on all the rest of the ticket_id's
任何想法 谢谢
推荐答案
您在寻找"SELECT * FROM temp_tickets GROUP BY ticket_id ORDER BY ticket_id
吗?
更新
SELECT t.*
FROM
(SELECT ticket_id, MAX(id) as id FROM temp_tickets GROUP BY ticket_id) a
INNER JOIN temp_tickets t ON (t.id = a.id)
这篇关于MySQL选择不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!