Use Rand and randomly assign to all columns

Asked

Viewed 41 times

0

I have this table (centrodb.Infocontributors) that has the ids of all the other tables I do the join in the database with this data:

inserir a descrição da imagem aquiinserir a descrição da imagem aqui

I have this query:

SELECT B.NomeColaborador, 
   C.Ala,
   D.Grupo,
   E.Turno

FROM centrodb.RelacaoColab AS A LEFT OUTER JOIN centrodb.InfoColaboradores AS B

ON B.Id = A.IdColab LEFT OUTER JOIN centrodb.TiposAlas AS C

ON C.Id = A.IdAla LEFT OUTER JOIN centrodb.TiposGrupos AS D

ON D.Id = A.IdGrup LEFT OUTER JOIN centrodb.H_Turno AS E

ON E.Id = A.IdTurn

WHERE E.Turno = 'M' ORDER BY RAND() LIMIT 8

How do I not show up repeated these situations when generating the 8 employees of the 11 I have in the database table?

1 answer

1

In order for the names not to be repeated simply put the end of the query before the order:

GROUP BY B.NomeColaborador

the issue of other duplicated information, I believe it is by the use of

left joins

See how left Join works:

inserir a descrição da imagem aqui

or it will bring the information from the left table if it is not equal. Just try to use inner join:

inserir a descrição da imagem aqui

place where I picked up the images codeacademy

How does your query look?

SELECT B.NomeColaborador, 
   C.Ala,
   D.Grupo,
   E.Turno

FROM centrodb.RelacaoColab AS A JOIN centrodb.InfoColaboradores AS B

ON B.Id = A.IdColab JOIN centrodb.TiposAlas AS C

ON C.Id = A.IdAla JOIN centrodb.TiposGrupos AS D

ON D.Id = A.IdGrup JOIN centrodb.H_Turno AS E

ON E.Id = A.IdTurn

WHERE E.Turno = 'M' GROUP BY B.NomeColaborador ORDER BY RAND() LIMIT 8
  • But if you do not put the other names of the columns that are in the SELECT gives error and when placing repeats the names of employees, the wings and the group

Browser other questions tagged

You are not signed in. Login or sign up in order to post.