我遇到查询问题。我有4张桌子
first table: person
ID_No
-----
1
2
3
second table: update_general_details
Update_ID ID_No Email Mob_No
--------- ----- -------------- --------
1 1 some@gmail.com 078231231
third table: update_training
Training_ID ID_No Training_Name Training-Date
----------- ---- ------------- -------------
1 2 Mysql training 2014-09-09
third table: update_award
Award_ID ID_No Award_Name Award_Year
-------- ----- ----------- ----------
1 1 Best in Math 2010
我要做的是选择不同的ID号,其中ID号可以包含在第二个表中,或者第三个或第四个。 期望的结果:
ID_No
1
2
这是我的代码:
Select DISTINCT person.ID_No,
from update_award, update_general, update_training, person
where EXISTS (SELECT update_award.ID_No
FROM update_award, person
WHERE person.ID_No = update_award.ID_No) or
EXISTS (SELECT update_training.ID_No
FROM update_training, person
WHERE person.ID_No = update_training.ID_No) or
EXISTS (SELECT update_general_details.ID_No
FROM update_general, person
WHERE person.ID_No = update_general.ID_No)
此查询不返回任何内容。提前谢谢
答案 0 :(得分:0)
如果你唯一需要的是ID,你可以跳过*
表,直接查询其他三个。使用person
将删除所有重复项:
union
答案 1 :(得分:0)
请从子查询中删除主查询和人员表中的详细信息表,这应该会有很大帮助:
Select person.ID_No
from person
where EXISTS (
SELECT 1
FROM update_award
WHERE person.ID_No = update_award.ID_No
)
or EXISTS (
SELECT 1
FROM update_training
WHERE person.ID_No = update_training.ID_No
) or EXISTS (
SELECT 1
FROM update_general
WHERE person.ID_No = update_general.ID_No
);