当前位置:Gxlcms > 数据库问题 > mysql exists子查询

mysql exists子查询

时间:2021-07-01 10:21:17 帮助过:16人阅读

exists后面的子查询,exists 只关心有没有,返回布尔类型

#案例:查询有员工的部门名

    SELECT department_name
FROM departments d
WHERE EXISTS(
    SELECT *
    FROM employees e
    WHERE d.`department_id`=e.`department_id`
);

SELECT department_name
FROM departments
WHERE department_id IN(
    SELECT department_id
    FROM employees
);

#查询没有女朋友的男神信息

SELECT bo.*
FROM boys bo
WHERE  NOT EXISTS(
    SELECT b.boyfriend_id
    FROM beauty b
    WHERE b.`boyfriend_id`=bo.`id`
);

SELECT bo.*
FROM boys bo
WHERE bo.`id` NOT IN(
    SELECT boyfriend_id
    FROM beauty b
);

mysql exists子查询

标签:HERE   friend   mysql   employees   子查询   exists   select   where   end   

人气教程排行