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

Valg på tværs af flere tabeller med UNION

Tak for opdateringen af ​​forespørgslen; nu kan vi se, at WHERE-betingelsen kun anvendes på sidste UNION-forespørgsel. Du skal enten tilføj den WHERE-sætning til hver forespørgsel, eller indpak den som et undervalg og anvend WHERE-sætningen på den.

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

eller

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. sql geografi til dbgeography?

  2. Annoncering af MariaDB 10.2 Support - ClusterControl 1.5

  3. Sådan opretter du et Excel-dokument fra et Java-program ved hjælp af Apache POI

  4. Oracle fjerner ikke markører efter lukning af resultatsæt