suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Fehler bei der Sortierung des MySQL-Datums

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

Falsche Datumssortierung. Wie soll ich es schreiben?

P粉759451255P粉759451255266 Tage vor462

Antworte allen(2)Ich werde antworten

  • 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 进行排序。

    Antwort
    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() 函数用于将日期/时间戳转换为字符串,但这里不需要它,因为可以直接比较日期/时间戳列。

    Antwort
    0
  • StornierenAntwort