我有一个 SQL Server 查询:
(
SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
FROM k12_dms_contacts_institution_jobtitles
INNER JOIN k12_dms_institution_master ON k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
WHERE (
k12_dms_institution_master.state_id='63'
AND k12_dms_contacts_institution_jobtitles.glevel_id='1'
)
GROUP BY
k12_dms_contacts_institution_jobtitles.job_title_id
)
INTERSECT
(
SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
FROM k12_dms_contacts_institution_jobtitles
INNER JOIN k12_dms_institution_master ON k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
WHERE (
k12_dms_institution_master.state_id='63'
AND k12_dms_contacts_institution_jobtitles.glevel_id='2'
)
GROUP BY
k12_dms_contacts_institution_jobtitles.job_title_id
)
INTERSECT
(
SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
FROM k12_dms_contacts_institution_jobtitles
INNER JOIN k12_dms_institution_master ON k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
WHERE (
k12_dms_institution_master.state_id='63'
AND k12_dms_contacts_institution_jobtitles.glevel_id='3'
)
GROUP BY
k12_dms_contacts_institution_jobtitles.job_title_id
)
INTERSECT
(
SELECT k12_dms_contacts_institution_jobtitles.job_title_id AS id
FROM k12_dms_contacts_institution_jobtitles
INNER JOIN k12_dms_institution_master ON k12_dms_institution_master.id = k12_dms_contacts_institution_jobtitles.inst_id
WHERE (
k12_dms_institution_master.state_id='63'
AND k12_dms_contacts_institution_jobtitles.glevel_id='4'
)
GROUP BY
k12_dms_contacts_institution_jobtitles.job_title_id
)
有什么办法可以优化上面的查询。交集只需要在Where子句的基础上。我想获取所有年级共有的所有职位。