mysql 等值连接案例之添加筛选、分组、排序、多表

发布时间:2020-06-10 03:01:33 作者:wx5d21d5e6e5ab1
来源:网络 阅读:329

加筛选

#案例:有奖金的员工名和部门名

SELECT 
    last_name,department_name
FROM
    employees AS e,departments AS d
WHERE
    e.department_id=d.department_id
AND
    e.commission_pct IS NOT NULL;

#案例:查询城市名中第二个字符为o的部门名和城市名

SELECT
    department_name,city
FROM
    departments AS d,locations AS l
WHERE
    d.location_id=l.location_id
AND
    l.city LIKE'_o%';

加分组

#案例:查询每个城市的部门个数

SELECT
    COUNT(*),city
FROM
    departments AS d,locations AS l
WHERE
    d.`location_id`=l.`location_id`
GROUP BY
    city;

#案例:查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资

SELECT
    department_name,d.manager_id,MIN(salary)
FROM
    employees AS e,departments AS d
WHERE
    d.department_id=e.department_id
AND
    commission_pct IS NOT NULL
GROUP BY
    department_name,d.manager_id;

加排序
#案例:查询每个工种的工种名和员工的个数,并且按员工个数降序

SELECT
    job_title,COUNT(*)
FROM
    employees AS e,jobs AS j
WHERE
    e.job_id=j.job_id
GROUP BY
    job_title
ORDER BY
    COUNT(*) DESC;

三表连接
#案例:查询员工名 、部门名、所在的城市

SELECT
    last_name,department_name,city
FROM
    employees AS e,departments AS d,locations AS l
WHERE
    e.department_id=d.`department_id`
AND
    d.`location_id`=l.location_id
推荐阅读:
  1. MySQL 查询语句select讲解与练习
  2. Mysql数据库条件查询语句如何使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:esproc vs python 1 增删改查

下一篇:windows下搭建seafile服务器,拥有自己的个人云盘,不再担心网盘丢

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》