sql >> Database >  >> RDS >> Mysql

Selecteren over meerdere tabellen met UNION

Bedankt voor het bijwerken van de vraag; nu kunnen we zien dat de WHERE-voorwaarde alleen wordt toegepast op de laatste UNIONed-query. U moet ofwel voeg die WHERE-component toe aan elke query, of wikkel deze in als een subselect en pas de WHERE-component daarop toe.

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

of

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


  1. Bereken procentuele toename/afname ten opzichte van de vorige rijwaarde

  2. SELECT en lijst kinderen en ouder

  3. TAN() Voorbeelden in SQL Server

  4. 5 snelle handige tips voor SQL Server Production DBA's