[英]PHP/MYSQL Distinct operator with multiple fields and field selection
I have a MYSQL table that looks like: +----+--------------+------------+----------------+---------------------+ | id | id_solicitud | id_estatus | id_responsable | fecha_hora | +----+--------------+------------+----------------+---------------------+ | 1 | 1 | 1 | 3 | 2017-11-20 11:12:05 | | 3 | 1 | 2 | 3 | 2017-11-20 13:30:50 | | 4 | 2 | 1 | 5 | 2017-11-20 13:42:35 | | 5 | 2 | 2 | 5 | 2017-11-20 13:52:25 | | 6 | 1 | 4 | 2 | 2017-11-21 12:20:17 | | 8 | 2 | 3 | 2 | 2017-11-21 13:39:57 | | 9 | 2 | 2 | 5 | 2017-11-21 13:40:55 | | 10 | 2 | 4 | 2 | 2017-11-21 13:45:24 | +----+--------------+------------+----------------+---------------------+
我有一个MYSQL表,看起来像:
+----+--------------+------------+----------------+---------------------+ | id | id_solicitud | id_estatus | id_responsable | fecha_hora | +----+--------------+------------+----------------+---------------------+ | 1 | 1 | 1 | 3 | 2017-11-20 11:12:05 | | 3 | 1 | 2 | 3 | 2017-11-20 13:30:50 | | 4 | 2 | 1 | 5 | 2017-11-20 13:42:35 | | 5 | 2 | 2 | 5 | 2017-11-20 13:52:25 | | 6 | 1 | 4 | 2 | 2017-11-21 12:20:17 | | 8 | 2 | 3 | 2 | 2017-11-21 13:39:57 | | 9 | 2 | 2 | 5 | 2017-11-21 13:40:55 | | 10 | 2 | 4 | 2 | 2017-11-21 13:45:24 | +----+--------------+------------+----------------+---------------------+
+----+--------------+------------+----------------+---------------------+ | id | id_solicitud | id_estatus | id_responsable | fecha_hora | +----+--------------+------------+----------------+---------------------+ | 1 | 1 | 1 | 3 | 2017-11-20 11:12:05 | | 3 | 1 | 2 | 3 | 2017-11-20 13:30:50 | | 4 | 2 | 1 | 5 | 2017-11-20 13:42:35 | | 5 | 2 | 2 | 5 | 2017-11-20 13:52:25 | | 6 | 1 | 4 | 2 | 2017-11-21 12:20:17 | | 8 | 2 | 3 | 2 | 2017-11-21 13:39:57 | | 9 | 2 | 2 | 5 | 2017-11-21 13:40:55 | | 10 | 2 | 4 | 2 | 2017-11-21 13:45:24 | +----+--------------+------------+----------------+---------------------+
Then, I proceed to filter by the id_solicitud field with the following Mysql QUERY: 然后,我继续使用以下Mysql QUERY按id_solicitud字段进行过滤:
SELECT id_estatus, id_responsable, fecha_hora FROM estatus_historico WHERE id_solicitud =2 ORDER BY fecha_hora 从estatus_historico处选择id_estatus,id_responsable,fecha_hora id_solicitud = 2按fecha_hora排序
The result: +------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
结果:
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
I want to apply the distinct operator over id_estatus column but selecting the older row when duplicate rows are found, ie my desired result is: +------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
我想对id_estatus列应用唯一的运算符,但是当发现重复的行时选择较旧的行,即我想要的结果是:
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 3 | 2 | 2017-11-21 13:39:57 | | 2 | 5 | 2017-11-21 13:40:55 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
I tried: 我试过了:
SELECT id_estatus, id_responsable, fecha_hora FROM estatus_historico WHERE id_solicitud =2 GROUP BY id_estatus ORDER BY fecha_hora 从estatus_historico中选择id_estatus,id_responsable,fecha_hora id_solicitud = 2 GROUP BY id_estatus ORDER BY fecha_hora
But the result is wrong: +------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
但是结果是错误的:
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
+------------+----------------+---------------------+ | id_estatus | id_responsable | fecha_hora | +------------+----------------+---------------------+ | 1 | 5 | 2017-11-20 13:42:35 | | 2 | 5 | 2017-11-20 13:52:25 | | 3 | 2 | 2017-11-21 13:39:57 | | 4 | 2 | 2017-11-21 13:45:24 | +------------+----------------+---------------------+
Any help would be much appreciated 任何帮助将非常感激
UPDATE: Files to create table and insert data => estatus_historico.sql 更新:用于创建表和插入数据的文件=> estatus_historico.sql
多亏了Rahul Shrivastava ,我终于找到了正确的答案:
SELECT * FROM ( SELECT id_estatus, id_responsable, MAX( fecha_hora ) AS fecha FROM estatus_historico WHERE id_solicitud =2 GROUP BY id_estatus ORDER BY fecha_hora ) AS MY_TABLE ORDER BY fecha
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.