sql >> Base de Datos >  >> RDS >> Mysql

Seleccionar en varias tablas con UNION

Gracias por actualizar la consulta; ahora podemos ver que la condición WHERE solo se aplica al último Consulta unida. Tienes que cualquiera agregue esa cláusula WHERE a cada consulta, o envuélvala como una subselección y aplique la cláusula WHERE a eso.

select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
where email = "[email protected]"
union
select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
where email = "[email protected]"
union
select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t
where email = "[email protected]"

o

SELECT * FROM (
select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
union
select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
union
select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t
) foo where email = "[email protected]"