Mysql select distinct(Mysql 选择不同)
问题描述
我正在尝试选择 mysql 表中的重复行,它对我来说工作正常,但问题是它不允许我选择该查询中的所有字段,只是让我选择我用作不同的字段名称,让我编写查询以更好地理解
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 7,8,9 上出现了 3 次,所以我想用最新的条目选择一次,在这种情况下为 9,这适用于所有其余的ticket_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 选择不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysql 选择不同
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
