mysql exists子查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql exists子查询相关的知识,希望对你有一定的参考价值。
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子查询的主要内容,如果未能解决你的问题,请参考以下文章