MYSQL情况未满足

时间:2019-11-15 09:49:00

标签: mysql

我有一个 Order_Table 表。该表列就是这样

Order Code | Code |     Create Time     |      Pick Time       |      Pack Time
Order1       A01    2019-11-15 09:25:30    2019-11-15 13:10:23   2019-11-15 14:51:36
Order2       A02    2019-11-15 15:33:31    2019-11-15 15:48:14           NULL
Order3       A03    2019-11-14 18:00:30          NULL                    NULL

创建时间是创建订单的时间, 提货时间是商店提货处理订单的时间, 打包时间是商店打包发送订单的时间

所以我需要创建一个报告才能变成这样

Order Code | Code |  Last Update Time  |
Order1       A01    2019-11-15 14:51:36
Order2       A02    2019-11-15 15:48:14         
Order3       A03    2019-11-14 18:00:30

最新更新时间是订单中的最新状态时间(例如, Order1 最新状态为包装,因此最新更新时间将获得订单1 中的打包时间)。这是我的查询:

SELECT
   order_code
   , code
   , CASE
        WHEN pack_time IS NOT NULL THEN pack_time
        WHEN pack_time IS NULL THEN pick_time
        WHEN pick_time IS NULL THEN create_time
        ELSE '-'
     END AS 'Last Update Time'
FROM Order_Table

即使 pack_time pick_time ,我的查询似乎也无法获得 Order3 创建时间值>为空。它甚至没有抓住ELSE声明。

Order Code | Code |  Last Update Time  |
Order1       A01    2019-11-15 14:51:36
Order2       A02    2019-11-15 15:48:14         
Order3       A03          NULL

2 个答案:

答案 0 :(得分:2)

您可以使用COALESCE()

    SELECT
       order_code,
       code,
       COALESCE(pack_time, pick_time, create_time) AS 'Last Update Time'
    FROM order

答案 1 :(得分:1)

一种选择是将<script type="text/javascript" src="../SiteAssets/JS/angular.min.js"></script> <script type="text/javascript" src="../SiteAssets/JS/angular-sanitize.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.min.js"></script> <script type="text/javascript" src="../SiteAssets/JS/angular-material.min.js"></script> 标量函数与 # Import from django.core.cache import cache from django.core.paginator import InvalidPage from django.views.generic.list import ListView from django.http Http404 class MyListView(ListView): template_name = 'MY TEMPLATE NAME' model = MY POST MODEL paginate_by = 10 def paginate_queryset(self, queryset, page_size): """Paginate the queryset""" paginator = self.get_paginator( queryset, page_size, orphans=self.get_paginate_orphans(), allow_empty_first_page=self.get_allow_empty()) page_kwarg = self.page_kwarg page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1 try: page_number = int(page) except ValueError: if page == 'last': page_number = paginator.num_pages else: raise Http404(_("Page is not 'last', nor can it be converted to an int.")) try: page = paginator.page(page_number) cache_key = 'mylistview-%s-%s' % (page_number, page_size) retreive_cache = cache.get(cache_key) if retreive_cache is None: print('re-caching') retreive_cache = super(MyListView, self).paginate_queryset(queryset, page_size) # Caching for 1 day cache.set(cache_key, retreive_cache, 86400) return retreive_cache except InvalidPage as e: raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { 'page_number': page_number, 'message': str(e) }) 一起使用,当GREATEST时,三个日期分别推迟到另外两个日期,顺序为:

COALESCE