搜尋

首頁  >  問答  >  主體

Mysql日期排序有誤

SELECT
co.id,
IFNULL( oe.follow_status, 'unFollow' ) AS followStatus,
co.due_time 
FROM
car_order co
LEFT JOIN order_expire oe ON oe.order_id = co.id 
WHERE
co.type = '贷款' 
AND co.STATUS = '已放款' 
AND co.approve_status != '结清成功' 
AND date_FORMAT( co.due_time, '%Y-%m-%d' ) <= '2022-10-29' 
ORDER BY
field( oe.follow_status, 'unFollow', 'follow' ),
co.due_time DESC,
co.id DESC

日期排序錯誤。該怎麼寫呢?

P粉759451255P粉759451255266 天前461

全部回覆(2)我來回復

  • P粉440453689

    P粉4404536892024-04-05 11:17:25

    SELECT
    co.id,
    IFNULL( oe.follow_status, 'unFollow' ) AS followStatus,
    co.due_time 
    FROM
    car_order co
    LEFT JOIN order_expire oe ON oe.order_id = co.id 
    WHERE
    co.type = '贷款' 
    AND co.STATUS = '已放款' 
    AND co.approve_status != '结清成功' 
    AND co.due_time <= '2022-10-29' 
    ORDER BY
    field( followStatus, 'unFollow', 'follow' ),
    co.due_time DESC,
    co.id DESC

    您應該改為透過 IFNULL 別名 followStatus 進行排序。

    回覆
    0
  • P粉064448449

    P粉0644484492024-04-05 00:21:23

    假設 due_time 是時間戳列,您應該能夠直接將其與日期文字進行比較:

    SELECT co.id,
           IFNULL(oe.follow_status, 'unFollow') AS followStatus,
           co.due_time 
    FROM car_order co
    LEFT JOIN order_expire oe ON oe.order_id = co.id 
    WHERE co.type = '贷款' AND 
          co.STATUS = '已放款' AND
          co.approve_status != '结清成功' AND
          co.due_time <= '2022-10-29' 
    ORDER BY FIELD(oe.follow_status, 'unFollow', 'follow'),
             co.due_time DESC,
             co.id DESC;
    

    DATE_FORMAT() 函數用於將日期/時間戳記轉換為字串,但這裡不需要它,因為可以直接比較日期/時間戳列。

    回覆
    0
  • 取消回覆