V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
lusheldon
V2EX  ›  MySQL

mysql 中如何实现这样的查询

  •  
  •   lusheldon · 2017-06-21 21:56:10 +08:00 · 3288 次点击
    这是一个创建于 2499 天前的主题,其中的信息可能已经有所发展或是发生改变。
    mysql>desc salaries
    +-----------+---------+------+-----+---------+-------+
    | Field | Type | Null | Key | Default | Extra |
    +-----------+---------+------+-----+---------+-------+
    | emp_no | int(11) | NO | PRI | NULL | |
    | salary | int(11) | NO | | NULL | |
    | from_date | date | NO | PRI | NULL | |
    | to_date | date | NO | | NULL | |
    +-----------+---------+------+-----+---------+-------+

    某员工的信息
    +--------+--------+------------+------------+
    | emp_no | salary | from_date | to_date |
    +--------+--------+------------+------------+
    | 111400 | 72446 | 1985-01-01 | 1986-01-01 |
    | 111400 | 100416 | 1999-12-29 | 2000-12-28 |
    | 111400 | 99971 | 2000-12-28 | 2001-12-28 |
    | 111400 | 103244 | 2001-12-28 | 9999-01-01 |
    +--------+--------+------------+------------+


    查询所有曾经降过薪的员工 emp_no 以及降薪幅度
    10 条回复    2017-06-22 09:20:19 +08:00
    hustlike
        1
    hustlike  
       2017-06-21 21:59:16 +08:00
    降过多次怎么算
    lusheldon
        2
    lusheldon  
    OP
       2017-06-21 22:02:19 +08:00 via Android
    @hustlike 降多次的情况下,应该也是可以查的吧。咱们先假设每个人最多可能降薪一次吧。
    reus
        3
    reus  
       2017-06-21 22:06:59 +08:00   ❤️ 1
    select
    distinct emp_no
    from salaries a
    join salaries b
    on a.emp_no = b.emp_no and a.to_date > b.to_date and a.salary < b.salary
    lusheldon
        4
    lusheldon  
    OP
       2017-06-21 22:09:57 +08:00 via Android
    @reus 这个可能就是我要的答案,不熟悉数据库,没想过可以自己 join 自己。感谢!
    reus
        5
    reus  
       2017-06-21 22:14:43 +08:00
    @lusheldon 但这个降薪幅度要计算的话,还得判断一下 a 和 b 的时间段是不是相邻的
    lusheldon
        6
    lusheldon  
    OP
       2017-06-21 22:19:54 +08:00 via Android
    @reus 加上条件 a.from_date=b.to_date 就能判断了吧。
    reus
        7
    reus  
       2017-06-21 22:23:31 +08:00   ❤️ 1
    @lusheldon 如果没有断的话,是的
    lusheldon
        8
    lusheldon  
    OP
       2017-06-21 22:27:04 +08:00
    select a.emp_no,b.salary-a.salary, a.to_date
    from salaries a join salaries b
    on a.emp_no=b.emp_no and a.to_date>b.to_date and a.salary<b.salary and a.from_date=b.to_date;
    奉上完整答案,再次感谢 @reus
    wwww961h
        9
    wwww961h  
       2017-06-21 22:29:23 +08:00
    用 join,把一个表当两个表用,特别简单
    sohucw
        10
    sohucw  
       2017-06-22 09:20:19 +08:00
    select a.emp_no,b.salary-a.salary, a.to_date
    from salaries a join salaries b
    on a.emp_no=b.emp_no and a.to_date>b.to_date and a.salary<b.salary and a.from_date=b.to_date; 正解啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   971 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:16 · PVG 05:16 · LAX 14:16 · JFK 17:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.