我有一个LEFT连接的查询,它在phpmyadmin中工作正常,但同样的查询为节点服务器中的某些行返回null。我是节点和MySQL的新手。
以下是我在日期范围内获取最高销售类别的查询:
SELECT o.order_id,c.category_id,c.category_name as Name, o.book_id, o.date_of_purchase, SUM(o.quantity) as Total, o.payment_method
FROM `order_details` as o
LEFT JOIN book as b on b.book_id = o.book_id
LEFT JOIN category as c on c.category_id = b.book_id
GROUP BY c.category_id
ORDER BY Total DESC LIMIT 1
我在这里做错了吗?
以下是我的表格和查询结果:
节点中相同的结果是:
{
"data": [
{
"order_id": 20,
"category_id": null,
"Name": null,
"book_id": 6,
"date_of_purchase": "2017-07-23T13:22:37.000Z",
"Total": 55,
"payment_method": "NB"
}
],
"status": 1,
"message": "Success"
}
我调用查询的节点代码:
/ *获得最高销售类别* /
category.getHighestSelling = function(fromDate, toDate) {
var query = "SELECT min(o.order_id) , c.category_id , c.category_name as Name , o.book_id , min(o.date_of_purchase) , SUM(o.quantity) as Total , min(o.payment_method) " +
"FROM `order_details` as o " +
"LEFT JOIN book as b on b.book_id = o.book_id " +
"LEFT JOIN category as c on c.category_id = b.book_id " +
"GROUP BY c.category_id , o.book_id " +
"ORDER BY Total DESC LIMIT 1 ";
if (fromDate && toDate) {
query += "WHERE `date_of_purchase` BETWEEN '" + dateFormat(fromDate, "yyyy-mm-dd , h:MM:ss ") + "' AND '" + (dateFormat(toDate, "yyyy-mm-dd , h:MM:ss ") || new Date()) + "'";
}
return utilities.executeQuery(query);
};