V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · Jun 21, 2017 · 4196 views
    This topic created in 3232 days ago, the information mentioned may be changed or developed.
    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 replies    2017-06-22 09:20:19 +08:00
    hustlike
        1
    hustlike  
       Jun 21, 2017
    降过多次怎么算
    lusheldon
        2
    lusheldon  
    OP
       Jun 21, 2017 via Android
    @hustlike 降多次的情况下,应该也是可以查的吧。咱们先假设每个人最多可能降薪一次吧。
    reus
        3
    reus  
       Jun 21, 2017   ❤️ 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
       Jun 21, 2017 via Android
    @reus 这个可能就是我要的答案,不熟悉数据库,没想过可以自己 join 自己。感谢!
    reus
        5
    reus  
       Jun 21, 2017
    @lusheldon 但这个降薪幅度要计算的话,还得判断一下 a 和 b 的时间段是不是相邻的
    lusheldon
        6
    lusheldon  
    OP
       Jun 21, 2017 via Android
    @reus 加上条件 a.from_date=b.to_date 就能判断了吧。
    reus
        7
    reus  
       Jun 21, 2017   ❤️ 1
    @lusheldon 如果没有断的话,是的
    lusheldon
        8
    lusheldon  
    OP
       Jun 21, 2017
    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  
       Jun 21, 2017
    用 join,把一个表当两个表用,特别简单
    sohucw
        10
    sohucw  
       Jun 22, 2017
    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; 正解啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5606 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 07:21 · PVG 15:21 · LAX 00:21 · JFK 03:21
    ♥ Do have faith in what you're doing.