Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取MySQL数据库中每月的条目数_Mysql_Database_Date_Count_Pivot - Fatal编程技术网

获取MySQL数据库中每月的条目数

获取MySQL数据库中每月的条目数,mysql,database,date,count,pivot,Mysql,Database,Date,Count,Pivot,我有一个MySQL数据库,其中包含两列预订: BookingDate (datetime) Status (varchar with the values of Confirmed, Open, Unconfirmed, Closed, Canceled) 我想统计每月状态为“关闭”、“打开”或“已确认”的条目数。稍后,我将使用PHP将这些数据导出到一个图表中,因此我需要在一个数组中传递它 在MySQL中实现这一点的最佳方法是什么?我希望结果是这样的: mm/yy -> # of ent

我有一个MySQL数据库,其中包含两列预订:

BookingDate (datetime)
Status (varchar with the values of Confirmed, Open, Unconfirmed, Closed, Canceled)
我想统计每月状态为“关闭”、“打开”或“已确认”的条目数。稍后,我将使用PHP将这些数据导出到一个图表中,因此我需要在一个数组中传递它

在MySQL中实现这一点的最佳方法是什么?我希望结果是这样的:

mm/yy -> # of entires
考虑:

select date_format(booking_date, '%m/%y'), count(*) nb_entries
from mytable
where status in ('Closed', 'Open', 'Confirmed')
group by year(booking_date), month(booking_date), date_format(booking_date, '%m/%y')
order by year(booking_date), month(booking_date)
如果希望每个状态有一列,则:

select 
    date_format(booking_date, '%m/%y'), 
    count(*) nb_entries,
    sum(status = 'Closed') nb_entries_closed,
    sum(status = 'Open') nb_entries_open,
    sum(status = 'Confirmed') nb_entries_confirmed
from mytable
where status in ('Closed', 'Open', 'Confirmed')
group by year(booking_date), month(booking_date), date_format(booking_date, '%m/%y')
order by year(booking_date), month(booking_date)

令人惊叹的!这完全行得通,还有一个问题-如果我再添加一行“城市”和“la”或“ny”选项,我将如何只选择la?@tony:
where status in('Closed'、'Open'、'Confirmed')和city='la'
再问你一个问题:如果我想做两个不同的输出。一个输出是这样的,另一个输出是已经关闭的预订,我该怎么做呢?所以基本上有mm/yy->关闭/打开/确认的数量->取消/未确认的数量!这完全可行,还有一个问题-如果我再添加一行“城市”和“la”或“ny”选项,我将如何只选择la?还有一个问题给你:如果我想要两个不同的输出。一个输出是这样的,另一个输出是关闭的预订,我该怎么做呢?所以本质上是mm/yy->关闭/打开/确认的数量->取消的数量/unconfirmed@tony再添加一个带有其他条件的输出字段:
SUM(状态为('cancelled','unconfirm'))
…以及如何按日期排序?从老到新?
SELECT DATE_FORMAT(booking_date, '%m/%y') `month and year`, 
       SUM(status IN ('Closed', 'Open', 'Confirmed')) `# of entires`
FROM `booking reservations`
-- WHERE city = 'LA'
GROUP BY DATE_FORMAT(booking_date, '%m/%y')