我有一个过去汽车预订的mysql表,其中包括以下列:
车辆类型(r_vehicle) - 预订月份(r_month)
我基本上需要找回这样的数据:
2月份Limo有3个销售,2月有2个等等。 轿车1月份销售5辆,2月份销售3辆等。我正在整合Charts.JS,它以这种方式询问数据:
<script>
var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var config = {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
datasets: [{
label: "Limo",
backgroundColor: window.chartColors.red,
borderColor: window.chartColors.red,
data: [
LOOP OVER SALES PER MONTH FOR LIMO
],
fill: false,
}, {
label: "Sedan",
fill: false,
backgroundColor: window.chartColors.blue,
borderColor: window.chartColors.blue,
data: [
LOOP OVER SALES PER MONTH FOR SEDAN
],
}]
}
如何编写查询,然后以Charts.js满意的方式迭代它? 仅供参考:我使用Coldfusion作为服务器端代码。 感谢您的任何指导!!!
答案 0 :(得分:3)
您可以使用r_vehicle的GROUP BY和r_month
轻松获取此数据SELECT r_vehicle, r_month, COUNT (*)
FROM reservations
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month;
如果您在缺少月份值的预订表中缺少值,则需要创建一个包含所有月份值的临时表,并使用预留表执行此表的LEFT JOIN。