21 noviembre, 2024

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
| Buscar en varias tablas con la misma estructura, es decir, con las mismas columnas. |
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Comando: [Execute query]
show tables;

Tables_in_DataBase <- Quitar el nombre del titulo de la columna
tabla1
tabla2
tabla3
tabla4
tabla5
^^^^^^^^^^^^^^^^^
|||||||||||||||||

Trabajar el resultado copiar/pegar SELECT * FROM …. UNION
|||||||||||||||||
vvvvvvvvvvvvvvvvv
SELECT * FROM tabla1 UNION
SELECT * FROM tabla2 UNION
SELECT * FROM tabla3 UNION
SELECT * FROM tabla4 UNION
SELECT * FROM tabla5 <- No poner UNION en el último registro
^^^^^^^^^^^^^^^^^
|||||||||||||||||

Agregar el SELECT * FROM ( … ) AS TablaTemporal
|||||||||||||||||
vvvvvvvvvvvvvvvvv
SELECT * FROM ( SELECT * FROM tabla1 UNION
SELECT * FROM tabla2 UNION
SELECT * FROM tabla3 UNION
SELECT * FROM tabla4 UNION
SELECT * FROM tabla5
) AS TablaTemporal
^^^^^^^^^^^^^^^^^
|||||||||||||||||

Agregar el WHERE
|||||||||||||||||
vvvvvvvvvvvvvvvvv
SELECT * FROM ( SELECT * FROM tabla1 UNION
SELECT * FROM tabla2 UNION
SELECT * FROM tabla3 UNION
SELECT * FROM tabla4 UNION
SELECT * FROM tabla5
) AS TablaTemporal
WHERE columna LIKE variable;
^^^^^^^^^^^^^^^^^
|||||||||||||||||

Agregar lo a un procedimiento
|||||||||||||||||
vvvvvvvvvvvvvvvvv
DROP PROCEDURE IF EXISTS getName;
CREATE PROCEDURE getName( variable VARCHAR(255))
SELECT * FROM ( SELECT * FROM tabla1 UNION
SELECT * FROM tabla2 UNION
SELECT * FROM tabla3 UNION
SELECT * FROM tabla4 UNION
SELECT * FROM tabla5
) AS TablaTemporal
WHERE columna LIKE variable;
CALL getName(‘%DatoABuscar%’);
^^^^^^^^^^^^^^^^^
|||||||||||||||||

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *