开发者

SQL to weekday instead of to_days?

开发者 https://www.devze.com 2023-04-04 20:18 出处:网络
I\'m trying to make a change this part of the sql (To_days( now( )) - TO_DAYS( o.date_payed)) days1, (To_days( no开发者_如何学JAVAw( )) - TO_DAYS( o.date_purchased)) days2,

I'm trying to make a change this part of the sql

(To_days( now( )) - TO_DAYS( o.date_payed)) days1,
(To_days( no开发者_如何学JAVAw( )) - TO_DAYS( o.date_purchased)) days2,

This works fine, but I'm trying to make two changes, the first one is that I would like to this be just days1 and if the result of To_days( now( )) - TO_DAYS( o.date_payed) is empty use the second one.

The second change would be to instead of using To_days some way to use only weekdays, any ideas are welcome.


For the first question you can use IF function, ie

SELECT IF((o.date_payed IS NOT NULL)&&(To_days(now()) != TO_DAYS(o.date_payed))
          ,To_days(now()) - TO_DAYS(o.date_payed)
          ,To_days(now()) - TO_DAYS(o.date_purchased)
        ) AS days1 ...

For the second part you can use the date_format function, ie DATE_FORMAT(o.date_payed, '%w').

0

精彩评论

暂无评论...
验证码 换一张
取 消