mysql - 统计在接下来 7 天内同一日期预订的所有人数

标签 mysql sql database

我已经建立了一个一日游预订系统,该系统收集预订人数 - 成人和 child ,当然还有旅行日期。旅游总人数上限为 40 人。

在我的“预订”表中,我存储了“ID”、“tourDate”、“erwachs”(成人)、“kinder”( child )和更多数据。

我现在拥有的是:

$query = "SELECT * FROM buchung
        where tourDatum >= curdate()
                and tourDatum <= curdate() + INTERVAL 7 DAY 
GROUP BY tourDatum";
$result = mysql_query($query);

$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"tourDatum");
$adult=mysql_result($result,$i,"erwachs");
$children=mysql_result($result,$i,"kinder");
$free=(40 - ($adult+$children));
echo "Tour Date - Spaces<br/>$first - $free <br/>";
$i++;}

但这并没有收集所有预订的空间

<小时/>

您好,感谢您的回答。不幸的是,这对我不起作用。上面的代码没有工作。我现在使用了以下代码:

$query = "SELECT * FROM buchung
where tourDate >= curdate()
and tourDate <= curdate() + INTERVAL 7 DAY GROUP BY tourDate";
$result = mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"tourDATE");
$adult=mysql_result($result,$i,"adults");
$children=mysql_result($result,$i,"children");
free=(40 - ($adult+$children));
echo "Tour Date - Spaces<br/>$first - $free <br/>";
$i++;}

但这并没有总结当天预订的所有人数。知道如何汇总给定日期之间的所有数字(按天分组)。

解释一下: 假设 2014 年 3 月 25 日

  • 一次预订:2 名成人和 1 名 child
  • 一次预订:4 名成人和 2 名 child

所以结果应该是:

6 名成人 + 3 名 child = 9 人。

那么当然要从最大人数40人中扣除,显示的结果应该是当天的31人。

最佳答案

SELECT
    date_format(tourDate,'%Y-%m-%d') AS 'tour date',
    (40 - (count(adults) + count(children))) AS 'free spaces'
FROM booking
where date_format(tourDate,'%Y-%m-%d') >= curdate()
    and date_format(tourDate,'%Y%m%d') <= curdate() + INTERVAL 7 DAY 
GROUP BY date_format(tourDate,'%Y-%m-%d')

关于mysql - 统计在接下来 7 天内同一日期预订的所有人数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22420708/

相关文章:

php - xampp mysql : select command denied

php - 如何从 CodeIgniter 在 mysql 中插入包含连字符/破折号的电子邮件?

php - 无法添加或更新子行 : a foreign key constraint fails

php - Doctrine 查询语言获取每组最大/最新行

java - 密码加密

mysql - 我如何在 SQL 中获取整个商店列表

sql - 如何在一个查询中使用 LIKE 和 IN 运算符?

mysql - 从单个表返回多个计数

database - 数据仓库中的时间点快照

sql-server - 在 SQL Server 表中匹配具有可变数量的前导零的字段