简体   繁体   中英

In what way can i replace INTERSECT in MySQL?

I have few tables(tables, faculty, department, subject etc.) Faculty contains Name field. All othes have primary keys and foreign keys. En each faculty there are some departments, in departments there are teachers, and each teacher has its post and each subject has its type, lecture, practicum ets. So i have search Names of faculties where^ strong text - s.name = 'Data bases' or s.name = 'C' and l.type = 'Lecture' - t.post ='Docent' Below is an example query using INTERSECT, but I need one that works in MySQL. I have tried to use INNER JOIN but it says "Coulmn "Name" in field list is ambigous.

select f.name
from faculty f, subject s, department d, teacher t, lecture l
where f.facpk=d.facfk and d.deppk=t.depfk and t.tchpk=l.tchfk and l.sbjfk=s.sbjpk and s.name = 'Data bases' or s.name = 'C' and l.type = 'Lecture'
intersect
select f.name
from faculty f, teacher t, department d
where f.facpk=d.facfk and d.deppk=t.depfk and t.post ='Docent';

one way is that to use Distinct and inner join:

SELECT DISTINCT 
    f.name
FROM
    faculty f
    INNER JOIN  department d
        ON f.facpk = d.facfk
    INNER JOIN  teacher t
        ON d.deppk = t.depfk
    INNER JOIN  lecture l
        ON  t.tchpk = l.tchfk 
    INNER JOIN subject s
        ON l.sbjfk = s.sbjp   
        AND (
            s.name = 'Data bases'
            OR (s.name = 'C' AND l.type = 'Lecture')
           )
    INNER JOIN  teacher t2
        ON d.deppk = t2.depfk
        AND t2.post = 'Docent';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM