Select 3 tables mysql

0

I would like to know how to do to bring the records of 3 tables in mysql:

Tabla1 ->Causa|Fecha|Tipo|Usuario
Tabla2 ->Causa|Fecha|Tipo|Usuario
Tabla3 ->Causa|Fecha|Tipo|Usuario

And I would like to bring a single listing Causa|Fecha|Tipo|Usuario with the content of the 3. But I do not know how to do the Select .

I clarify that the data in any of the 4 fields may be different.
Thank you very much already.

    
asked by roberto carrillo 15.05.2018 в 18:55
source

2 answers

5

For what you need you can use the operator UNION

SELECT Causa,Fecha,Tipo,Usuario FROM Tabla1
UNION
SELECT Causa,Fecha,Tipo,Usuario FROM Tabla2
UNION
SELECT Causa,Fecha,Tipo,Usuario FROM Tabla3;

The UNION operator is used to combine the result set of two or more SELECT instructions.

You must bear in mind that:

  • Each statement SELECT within UNION must have the same number of columns
  • Columns must also have similar data types
  • The columns in each statement SELECT must also be in the same order.

More info on the official MySQL documentation and W3Schools

    
answered by 15.05.2018 / 19:04
source
0

As far as you can understand or what I could understand, your solution could be made with the UNNION ALL clause an example would be the following:

(
SELECT
sonido.nombre AS S,
sonido.ruta AS R
FROM
sud_cat
INNER JOIN sonido ON sonido.id_cat = sud_cat.id_sub_cat
WHERE sud_cat.sub_cat = 'Intro'
ORDER BY RAND()
LIMIT 3
)
UNION ALL
(
SELECT
video.nombre AS S,
video.fech_sub AS R
FROM
sud_cat
INNER JOIN video ON video.id_cat = sud_cat.id_sub_cat
WHERE sud_cat.sub_cat = 'Video'
ORDER BY RAND()
LIMIT 3
)
UNION ALL 
(
SELECT
sonido.nombre AS S,
sonido.ruta AS R
FROM
sud_cat
INNER JOIN sonido ON sonido.id_cat = sud_cat.id_sub_cat
WHERE sud_cat.sub_cat = 'Fin'
ORDER BY RAND()
LIMIT 3
)

Something that you have to keep in mind is the following, if you can notice in my query I bring only 2 parameters to which I rename as S and R, this is very important, since they have to have the same header so that This can make the query and group it in the number of columns you establish.

The result in my case would be the following:

-- ----------------- Primera consulta ---------------
"Intro_07.mp3"  "./assets/archivos/Intro/Intro_07.mp3"
"Intro_08.mp3"  "./assets/archivos/Intro/Intro_08.mp3"
"Intro_13.mp3"  "./assets/archivos/Intro/Intro_13.mp3"
-- ----------------- Segunda consulta ---------------
"video_16.mp4"  "2018-05-12 16:57:03"
"video_21.mp4"  "2018-05-12 17:01:29"
"video_12.mp4"  "2018-04-19 13:48:22"
-- ----------------- Tercera consulta ---------------
"Fin_09.mp3"    "./assets/archivos/Fin/Fin_09.mp3"
"Fin_08.mp3"    "./assets/archivos/Fin/Fin_08.mp3"
"Fin_07.mp3"    "./assets/archivos/Fin/Fin_07.mp3"
    
answered by 15.05.2018 в 20:21