› 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
KagamineLenKai2
V2EX  ›  MySQL

SQL 苦手求 dalao 指点……

  •  
  •   KagamineLenKai2 · Aug 30, 2016 · 4197 views
    This topic created in 3686 days ago, the information mentioned may be changed or developed.

    各位 dalao ,我遇到了一个难题……
    我需要查找下图这样的一个表……

    首先先找到所有 before-status=10002200, end-status=10005100 的记录,然后找同一 apply-id ,向下找 ol-id 最近, before-status=10005100, end-status=10005200 的记录,输出这条记录的 operation-time as 接单时间。
    然后再找有没有同一 apply-id ,向下找 ol-id 最近, before-status=10005200, end-status=10002300 的记录,如果有的话输出这条记录的 operator-remark as 退回原因。 输出成下图这样的格式……(内容都是我瞎编的,大概是这样子)

    求 dalao 指点…搞了一下午也没想到该怎么写…
    怕口述不清,所以导了一份表, dalao 不嫌弃的话可以下载下来看一下,谢谢各位(/´Д`)/
    https://pan.baidu.com/s/1qYCg8Ja

    10 replies  •  2016-08-31 17:42:48 +08:00
    lxy42
        1
    lxy42  
       Aug 30, 2016
    我真的没看懂,可能是我对数据库不够熟
    TZ
        2
    TZ  
       Aug 30, 2016
    "向下找 ol-id 最近, " 这句话什么意思
    KagamineLenKai2
        3
    KagamineLenKai2  
    OP
       Aug 30, 2016
    @TZ 比如说 apply_id 12345 before-status=10002200, end-status=10005100 这条数据的 ol_id 是 99988 ,在这条数据下面有 ol_id 99989 和 ol_id99999 两条数据都符合 before-status=10005100, end-status=10005200 ,那么返回 ol_id 99989 这条数据的 operation-time
    binghe
        4
    binghe  
       Aug 30, 2016
    额。误入。晕头转向的出去了。
    wmttom
        5
    wmttom  
       Aug 30, 2016
    需求表述的实在是太不清楚了,如果只是要把两种拼一起 union 下吧。但是看意思好像要根据条件把同一个 apply_id 下的数据分层多个 session 来处理,这样偏复杂的业务逻辑还是把数据都读内存直接代码处理吧,一共就几兆数据,硬是用 SQL 写出来也低效难改难维护。

    话说直接把公司数据库扔网上随便让别人下真的没问题吗

    ``` sql
    (SELECT apply_id,operation_time as 接单时间, null as 退回原因
    FROM operation
    WHERE before_status = 10005100 AND end_status = 10005200)
    UNION
    (SELECT apply_id,operation_time as 接单时间, operator_remark as 退回原因
    FROM operation
    WHERE before_status = 10005200 AND end_status = 10002300)
    order by apply_id,接单时间
    ```
    heeryuy
        6
    heeryuy  
       Aug 30, 2016
    楼主的意思是取 before-status=10002200, end-status=10005100 在同一个 apply-id 中筛选出 ol-id 最小的一条记录?
    KagamineLenKai2
        7
    KagamineLenKai2  
    OP
       Aug 31, 2016
    @heeryuy 对的……
    lazyCoder
        8
    lazyCoder  
       Aug 31, 2016
    没看懂需求
    77alex
        9
    77alex  
       Aug 31, 2016
    select apply_id ,operation_time as 接单时间,operator_remark as 退回原因 from (select ol_id,apply_id,before_status,operator_remark,operation_time from `operation` where before_status in (10002200,10005200) order by apply_id,before_status) aa group by apply_id,before_status;

    这样?
    x8
        10
    x8  
       Aug 31, 2016
    试试存储过程呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2311 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:44 · PVG 19:44 · LAX 04:44 · JFK 07:44
    ♥ Do have faith in what you're doing.