seleccionar filas únicas basadas en una sola columna distinta
Quiero seleccionar filas que tengan un distinct email
, vea la tabla de ejemplo a continuación:
+----+---------+-------------------+-------------+
| id | title | email | commentname |
+----+---------+-------------------+-------------+
| 3 | test | [email protected] | rob |
| 4 | i agree | [email protected] | rob |
| 5 | its ok | [email protected] | rob |
| 6 | hey | [email protected] | rob |
| 7 | nice! | [email protected] | simon |
| 8 | yeah | [email protected] | john |
+----+---------+-------------------+-------------+
El resultado deseado sería:
+----+-------+-------------------+-------------+
| id | title | email | commentname |
+----+-------+-------------------+-------------+
| 3 | test | [email protected] | rob |
| 7 | nice! | [email protected] | simon |
| 8 | yeah | [email protected] | john |
+----+-------+-------------------+-------------+
Donde no me importa qué valor de columna id
se devuelve.
¿Cuál sería el SQL requerido?
4 answers
Uno rápido en TSQL
SELECT a.*
FROM emails a
INNER JOIN
(SELECT email,
MIN(id) as id
FROM emails
GROUP BY email
) AS b
ON a.email = b.email
AND a.id = b.id;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-16 14:06:14
Asumo que quiere decir que no le importa qué fila se utiliza para obtener la title
, id
, y commentname
valores (tiene "rob" para todas las filas, pero no se si eso es realmente algo que se aplicaría o no en su modelo de datos). Si es así, puede usar las funciones de ventana para devolver la primera fila de una dirección de correo electrónico determinada:
select
id,
title,
email,
commentname
from
(
select
*,
row_number() over (partition by email order by id) as RowNbr
from YourTable
) source
where RowNbr = 1
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-25 20:40:10
Dado que no le importa qué id devolver, me quedo con el ID MÁXIMO para cada correo electrónico para simplificar la consulta SQL, inténtelo
;WITH ue(id)
AS
(
SELECT MAX(id)
FROM table
GROUP BY email
)
SELECT * FROM table t
INNER JOIN ue ON ue.id = t.id
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-25 20:43:15
Si está utilizando MySQL 5.7 o posterior , de acuerdo con estos enlaces (MySQL Official, ASÍ QA ), podemos seleccionar un registro por group by
sin necesidad de ninguna función agregada.
Así que la consulta se puede simplificar a esto.
select * from comments_table group by commentname;
Pruebe la consulta en acción aquí
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:12