简体   繁体   中英

Retrieving records 10 mins before to the given datetime

Need to find records that are 10 minutes past from the given Date time. From the below query it gets the records 10 minutes earlier from the given date that which in-turn selects all the records from the previous or day before that.

As I'm expecting to retrieve all the records from the given date that is from 10 minutes earlier. But not all the records ie, previous days to the given date mentioned.

$sql = "SELECT otb.*
FROM oc_table_book otb
LEFT JOIN oc_order o
ON (o.order_id = otb.order_id)
WHERE otb.preorder_status_id = '3' AND 
otb.booked_date <= '" . $this->db->escape($table_data['booked_date']) . 
"' - INTERVAL 10 MINUTE ORDER BY otb.booked_date";

Tried with DATEADD() didn't work

$sql = "SELECT otb.*
FROM oc_table_book otb
LEFT JOIN oc_order o
ON (o.order_id = otb.order_id)
WHERE otb.preorder_status_id = '3' AND 
otb.booked_date <= dateadd(minute, -10, '" . $this->db->escape($table_data['booked_date']) . 
")";

Results in

Fatal error: Uncaught Exception: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2018-01-19 07:30:00)' at line 6<br />Error No: 1064<br />SELECT otb.* FROM oc_table_book otb LEFT JOIN oc_order o ON (o.order_id = otb.order_id) WHERE otb.preorder_status_id = '3' AND otb.booked_date <= dateadd(minute, -10, '2018-01-19 07:30:00) in /var/www/html/silverspoon/system/library/db/mysqli.php on line 40

You would appear to want:

SELECT otb.*
FROM oc_table_book otb JOIN  -- I don't see why LEFT JOIN is necessary
     oc_order o
     ON o.order_id = otb.order_id
WHERE otb.preorder_status_id = 3 AND -- probably an integer
      otb.booked_date <= ? - INTERVAL 10 MINUTE AND
      otb.booked_date >= DATE(?)
ORDER BY otb.booked_date;

Note the use of ? . This is a parameter placeholder. You should be using that for passing constant values into queries.

You have an Error in SQL Syntax

Try this Query :

SELECT otb.* FROM oc_table_book otb
LEFT JOIN oc_order o
ON o.order_id = otb.order_id
WHERE otb.preorder_status_id = '3' AND 
otb.booked_date <= dateadd(minute, -10,".$this->db->escape($table_data['booked_date']).")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM