我想要完成的是显示正确的结果。
我所拥有的是两个不同的表格,orders
和orders_products
。
我尝试将orders.order_date
和orders_products.order_date
相互匹配并排除秒数,因为数据库之间存在延迟,而且信息不会显示。
orders_products 示例。
id user_id user_name quantity product price order_date
19 12 Test 1 Advanced game overlay 20 2015-02-17 19:31:16
20 12 Test 1 Advanced standby overlay 20 2015-02-17 19:31:16
21 12 Test 1 Description images 15 2015-02-17 19:31:16
订单示例。
id user_id user_name user_twitch user_email specific_color specific style webcam_size monitor_resolution faster_delivery order_message order_date user_ip order_status order_paid
4 12 Test John Doe Test Test@hotmail.com Blue random information 1024x768 0 ... 2015-02-17 19:31:14 xx.xx.xx.xx 1 0
如果您比较这两个表格之间的order_date
,orders.order_date
的速度会慢2秒,且信息不会显示。
我试图展示一个"你的订单"页面,和SUM()每个订单的总和及其列出的产品。
$sth = $conn->prepare(
"SELECT DATE_FORMAT(order_date, '%Y %m %d %H:%i') AS date_convert "
. "FROM users, orders "
. "WHERE users.user_id = :user_id AND orders.user_id = :user_id"
);
$date = date('Y:m:d H:i', strtotime($result['order_date']));
$sth = $conn->prepare(
"SELECT SUM(price) AS price_sum "
. "FROM orders_products "
. "WHERE user_id = :user_id AND order_date = :order_date"
);
$sth->bindValue(':user_id', $row['user_id'], PDO::PARAM_INT);
$sth->bindValue(':order_date', $date, PDO::PARAM_STR);
答案 0 :(得分:0)
只需比较日期部分并忽略时间(并修复日期格式):
// Use Y-m-d as that is the format MySQL is expecting
$date = date('Y-m-d', strtotime($result['order_date']);
// Use DATE() to only use the date poprtion of the datetime value
$sth = $conn->prepare("SELECT SUM(price) AS price_sum FROM orders_products WHERE user_id = :user_id AND DATE(order_date) = :order_date");
$sth->bindValue(':user_id', $row['user_id'], PDO::PARAM_INT);
$sth->bindValue(':order_date', $date, PDO::PARAM_STR);