Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Auswahl über mehrere Tabellen hinweg mit UNION

Vielen Dank für die Aktualisierung der Abfrage. jetzt können wir sehen, dass die WHERE-Bedingung nur auf das letzte angewendet wird UNIONed-Abfrage. Sie müssen beides Fügen Sie diese WHERE-Klausel zu jeder Abfrage hinzu oder packen Sie sie in eine Unterauswahl und wenden Sie die WHERE-Klausel darauf an.

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

oder

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