Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql's subquery

Shulou Source: shulou.com Published: 2022-06-01 22:35:16 10月02日 Update

For the subquery after exists, exists only cares about whether there is one, and returns a Boolean type.

# case: query the name of the department with employees

SELECT department_nameFROM departments dWHERE EXISTS (SELECT * FROM employees e WHERE d.`department _ id` = e.`department _ id`)

Or

SELECT department_nameFROM departmentsWHERE department_id IN (SELECT department_id FROM employees)

# query the information of male gods who do not have girlfriends

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

Or

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

Tags: Query Information employee girlfriend Boolean case Type Department Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Shulou Technology Xiaomi Redmi