需要查找距离给定日期时间过去10分钟的记录。 从下面的查询中,它从给定日期开始提前10分钟获取记录,该日期又选择前一天或前一天的所有记录。
因为我期望从之前10分钟的给定日期检索所有记录。但并非所有记录,即提到的给定日期的前几天。
$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";
尝试使用DATEADD()无法正常工作
$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']) .
")";
结果
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
答案 0 :(得分:0)
您似乎想要:
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;
请注意?
的使用。这是一个参数占位符。您应该使用它将常量值传递给查询。
答案 1 :(得分:0)
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']).")