Query for daily before 11am with MySQL

430 Views Asked by At

I have a datetime field and want to get all entries before 11am. It is for an online store where you can order until 11:00am and it will be shipped the same day.

SELECT * 
FROM Tablename 
WHERE (order.date_added < STR_TO_DATE(YEAR(), MONTH(), DAY()'%Y %d,%M'), '11:00:00')

Unfortunately, I do not know how to assemble this statement.

2

There are 2 best solutions below

1
Gordon Linoff On BEST ANSWER

If you want orders before 11:00 a.m. today, you can use:

where o.date_added >= curdate() and
      time(o.date_added) < '11:00:00'
0
nbk On

This gives you the currentday at 11 o'clock

CurDATE() + INTERVAL  11 HOUR

And the query selects all orders between yesterday 11 and today eleven.

you should add also criteria to exclude all orders that are already made

CREATE tABLe orders (date_added DATETIME)
INSERT INTO orders VALUES (NOW() -INTERVAL 10 HOUR)
SELECT * 
FROM orders 
WHERE orders.date_added BETWEEN (CurDATE() - INTERVAL 1 DAY) + INTERVAL  11 HOUR AND  CurDATE() + INTERVAL  11 HOUR
| date_added          |
| :------------------ |
| 2021-07-08 06:40:50 |
SELECT CurDATE() + INTERVAL  11 HOUR
| CurDATE() + INTERVAL  11 HOUR |
| :---------------------------- |
| 2021-07-08 11:00:00           |

db<>fiddle here