OBTAIN FIRST RECORD THAT IS REPEATED IN A COLUMN (M: M)

0

Good afternoon!

I want to get the first id of a table M: M with select distint. select distinct is not working.

You can give me an orientation how to make the query, please!

SELECT DISTINCT tarea_actividad.idTarea

,tarea_actividad.idConsignacion

,tarea.ide_cliente

FROM tarea_actividad   

INNER JOIN tarea ON  tarea_actividad.idTarea=tarea.ide_tarea

INNER JOIN cliente ON tarea.ide_cliente=cliente.id

WHERE tarea_actividad.idTarea=333
asked by roca33scorpio 01.02.2018 в 23:01
source

2 answers

0

what happens is that you put the DISTINCT, as in the table is also the field tarea_actividad.idConsignacion , brings the idTarea = 333 and also the 4 field records task_activity.idConsignation . You can do it in several ways. In the first example what it does is to do the same query but it only returns 1 record. In the second, what is done is to group all the data in the field idConsignacion in this way

  

(4,15,16,17)

SELECT DISTINCT tarea_actividad.idTarea

,tarea_actividad.idConsignacion

,tarea.ide_cliente

FROM tarea_actividad   

INNER JOIN tarea ON  tarea_actividad.idTarea=tarea.ide_tarea

INNER JOIN cliente ON tarea.ide_cliente=cliente.id

WHERE tarea_actividad.idTarea=333 LIMIT 1
SELECT DISTINCT tarea_actividad.idTarea

,GROUP_CONCAT(tarea_actividad.idConsignacion)

,tarea.ide_cliente

FROM tarea_actividad   

INNER JOIN tarea ON  tarea_actividad.idTarea=tarea.ide_tarea

INNER JOIN cliente ON tarea.ide_cliente=cliente.id

WHERE tarea_actividad.idTarea=333
    
answered by 01.02.2018 в 23:41
0

If you want a single row using DISTINCT is redundant in this case.

By using WHERE tarea_actividad.idTarea=333 combined with LIMIT 1 , you will always get a single row.

So you can write your query like this:

SELECT 
    tarea_actividad.idTarea,
    tarea_actividad.idConsignacion,
    tarea.ide_cliente
FROM tarea_actividad   
INNER JOIN tarea ON  tarea_actividad.idTarea=tarea.ide_tarea
INNER JOIN cliente ON tarea.ide_cliente=cliente.id
WHERE tarea_actividad.idTarea=333
LIMIT 1;

For more details on LIMIT , you can consult the documentation .

    
answered by 02.02.2018 в 00:47