我有 2 个表,emp_master 和 passport_details。
emp_master(emp_id,first_name,email_id,dob,doj,status.........)
passport_details(id, emp_id,passport_number,given_name,......).
我正在尝试从没有输入 passport_details 的 emp_master 获取 emp_id 和 first_name。
我尝试了不同的子查询组合,使用了 NOT IN,NOT EXISTS。
SELECT emp_id,first_name
FROM emp_master
WHERE emp_id NOT IN(SELECT emp_id FROM passport_details WHERE status=1);
我遇到了错误
You have an error in your SQL syntax near 'SELECT emp_id FROM passport_details WHERE status=1)' at line 3
我使用的是 MySQL 3.23。
我的问题是
- Do MySQL 3.23 supports SubQueries?
- What could be the optimal query to fetch emp_id and first_name from emp_master who have not entered passport_details.
最佳答案
select em.emp_id, em.first_name
from emp_master em left join passport_details pd
on pd.emp_id = em.emp_id and pd.status = 1
where pd.emp_id is null
我没有要测试的 3.23 实例,但这应该可以。
关于MySQL 子查询 - MySQL 3.23,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322484/