Mysql
 sql >> Base de données >  >> RDS >> Mysql

Sélection sur plusieurs tables avec UNION

Merci d'avoir mis à jour la requête ; maintenant nous pouvons voir que la condition WHERE n'est appliquée qu'au dernier Requête UNIONnée. Vous devez soit ajoutez cette clause WHERE à chaque requête, ou encapsulez-la dans une sous-sélection et appliquez-y la clause WHERE.

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

ou

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