如何编写查询以根据条件检查另一个表?

时间:2016-01-20 11:58:45

标签: php mysql sql

我有一个order_status表,如下所示:

order_id   order_status
1          100
2          1000
3          1000

我有另一张表order_status_history,它存储订单的历史数据:

id  order_id   order_status  status_date
1   1          100           2016-01-01
2   2          100           2016-02-01
3   2          300           2016-04-01
4   3          100           2016-04-01
5   3          400           2016-04-01
6   3          1000          2016-04-01
7   2          1000          2016-05-01

我正在尝试构建每日报告,在给定日期的情况下,我想查看订单是否已完成或正在进行中。我对SQL很不好,也无法弄清楚如何做到这一点。如果是约会,订单的状态是< 1000然后它正在进行中,否则它就完成了。

例如:对于2016-04-01日期,报告将是:

order_id    status
1           in-progress (because as of report date, its latest status is 100)
2           in-progress (because as of report date, its latest status is 300)
3           completed   (because as of report date, its latest status is 1000)

我正在尝试以下内容尝试至少查找正在进行的订单:

select distinct s.order_id, s.order_status
from order_status 
    left join order_status_history h on h.order_id = s.order_id
where max(h.status_date) <'2016-04-01'
  and status < 1000

非常感谢任何帮助!

4 个答案:

答案 0 :(得分:1)

我不知道你的状态是什么意思。但对于给定的订单,它们似乎总是在增加。因此,最近的一个将具有最大值:

select osh.order_id, max(osh.order_status)
from order_status_history osh
group by osh.order_id;

如果增加状态的假设不成立,那么您可以使用聚合并加入:

select osh.*
from order_status_history osh join
     (select order_id, max(status_date) as maxsd
      from order_status_history
      group by order_id
     ) o
     on osh.order_id = osh.order_id;

至于文本描述,你应该真正将这些信息存储在一个引用表中并加入它。(我怀疑这是order_status是什么,但是描述得很差。)否则,你使用的是case声明:

select (case when osh.order_status = 100 then 'in-progress'
             when osh.order_status = 100 then 'in-progress'
             when osh.order_status = 1000 then 'completed'
        end)

答案 1 :(得分:0)

如果您想获得指定日期的order_id状态,可以使用下一个查询

SELECT OS.order_id,
        CASE WHEN 
            (SELECT MAX(OSH.order_status) 
             FROM order_status_history AS OSH
             WHERE OSH.order_id = OS.order_id
                       AND OSH.status_date <= @date)) < 1000
             THEN 'in-progress'
             ELSE 'completed' END status
FROM order_status AS OS

答案 2 :(得分:0)

试试这个

    select m.order_id,IF(order_done < order_status,'in-progress (because as of report date, its latest status is '||m.order_done,'completed (because as of report date, its latest status is '||m.order_done) status
    from order_status m left join (select osh.order_id, max(osh.order_status) order_done
    from order_status_history osh
where osh.status_date <'2016-04-01' and osh.status < 1000
    group by osh.order_id) n on m.order_id = n.order_id

答案 3 :(得分:0)

假设即使当前订单状态也记录在历史表中(基于订单1的数据):

select distinct s.order_id, s.order_status as current_order_status, if(h2.order_status<1000,'in progress','completed') as status_at_report_date
from order_status left join
    (select order_id, max(id) as max_id from order_status_history h1
    where h1.status_date<='2016-04-01') t on t.order_id=s.order_id
    left join order_status_history h2 on t.max_id=h2.id