Mysql
 sql >> Database >  >> RDS >> Mysql

Selezione su più tabelle con UNION

Grazie per aver aggiornato la query; ora possiamo vedere che la condizione WHERE viene applicata solo all'ultimo Query UNIONed. Devi uno aggiungi quella clausola WHERE a ogni query, o avvolgila come sottoselezione e applica la clausola WHERE a quella.

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]"