【问题标题】:hotel reservation query for booking date预订日期的酒店预订查询
【发布时间】:2012-11-02 01:54:37
【问题描述】:

如何将查询的结果放入数组中,结果应该比较每个结果并合并到一个数组中。

我有 3 个查询,我不知道我的编码有什么问题,我最好的还不够,所以请大家帮助我,尤其是 sql 和 php 专家。

这是我的代码:

        <?php
        $a = $p['id'];
        $query1 = mysql_query("SELECT id FROM prereservation where '$arrival' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        $rows1 = mysql_fetch_array($query1);  

        $query2 = mysql_query("SELECT id FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        $rows2 = mysql_fetch_array($query2);

        $query3 = mysql_query("SELECT id FROM prereservation where arrival > '$arrival' and departure < '$departure' and room_id ='$a' and status = 'active'");
        $rows3 = mysql_fetch_array($query3);

        $sample = array_unique(array_merge($rows1, $rows2, $rows3));             
        ?> 

显示是这样的:

        <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
        <option value="0"></option>
        <? $counter = 1; ?>
        <? while ($counter <= ($p['qty']) - $????????) { ?>
        <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
        <? $counter++;
        }?>
        </select>

我想要的例如输出是这样的:

$rows1 = array(id= 48, 55, 51, 53)
$rows2 = array(id= 48, 49, 51, 52)
$rows3 = array(id= 48, 49, 50, 51)
$sample = array_unique(array_merge($rows1, $rows2, $rows3))

所以样本的输出是这样的:

$sample = array(id= 48, 49, 50, 51, 52, 53)

那么在表格预订中,列是:'id', 'arrival', 'departure', 'room_id'、'数量'和'状态'。

在我的餐桌预订中,每个 id 都有 不同 *数量*,所以我想要 sum 个,共 qty 个,每个 id 的样本如下:

    id    |   qty
    48    |    2
    49    |    1
    50    |    1
    51    |    3   
    52    |    1
    53    |    3  


$total = sum(qty); which is 11

你们能帮我如何创建一个查询来获取每个 qtysum id 的?还请检查前 3 个查询,因为我知道每个查询都有一个 错误。获取 array_mergearray_unique。 非常感谢你们。

            <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
            <option value="0"></option>
            <? $counter = 1; ?>
            <? while ($counter <= ($p['qty']) - $total) { ?>
            <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
            <? $counter++;
            }?>
            </select>

这里是我的数据库:

table rooms
id |  type   |  qty
---|---------|-----
11 | single  |  50
12 | double  |  50
13 | deluxe  |  20

table preservation
id |   arrival   |  departure  | room_id | qty | status
---|-------------|-------------|---------|-----|-------
48 | 05/11/2012  | 11/11/2012  |    11   |  2  | active 
49 | 06/11/2012  | 11/11/2012  |    11   |  1  | active 
50 | 06/11/2012  | 08/11/2012  |    11   |  1  | active 
51 | 05/11/2012  | 07/11/2012  |    11   |  3  | active 
52 | 06/11/2012  | 09/11/2012  |    11   |  1  | active 
53 | 07/11/2012  | 07/11/2012  |    11   |  3  | active 

所以在我的显示中,我有一个来自 table rooms(id=11) 的选择标签,选项是 qtydeducted by preservation(room_id=11)

qty 的strong>总和

【问题讨论】:

  • 为什么不做一个 SQL 查询并用 OR 连接 where 子句?

标签: php mysql sql


【解决方案1】:

使用一条 SQL 语句供您选择,

"SELECT id, SUM(qty) FROM prereservation GROUP BY qty WHERE ( ( '$arrival' BETWEEN arrival and departure and room_id ='$a' ) OR ( '$departure' BETWEEN arrival and departure and room_id ='$a' ) OR ( arrival > '$arrival' and departure < '$departure' and room_id ='$a' ) ) AND status = 'active'";

编辑:

好的,这样的事情怎么样?

SELECT *, (SELECT SUM(preservation.qty) as sum FROM preservation WHERE rooms.id = preservation.room_id GROUP BY preservation.room_id ) as qty_reserved, rooms.qty - (SELECT SUM(preservation.qty) as sum FROM preservation WHERE rooms.id = preservation.room_id GROUP BY preservation.room_id ) as qty_available  FROM rooms;

访问数据库一次,获取所需的所有信息并在整个应用程序中重用这些信息,效率要高得多。如果只是显示这些变量,您的 php 中不需要任何逻辑。

【讨论】:

  • 我已经使用过这种查询,但问题是在这 3 个运算符范围内的每个 id,每个 id 的总和也是两倍、三倍或更高,因为它的总和将从中扣除我的另一个表称为房间,其中列有 room_id 和 qty。
  • 我认为获取这 3 个查询的 id 并使用 array_merge 和 array_unique 并获得结果 id 的数量总和要好得多。
  • 对不起,我在您的解释后迷路了,也许表格的例子会有所帮助?以及您想要的结果。
  • 请再次检查我放了我的桌子,抱歉我的英语不好,我无法简要解释。谢谢你帮助我
  • 对,所以你想知道还剩多少?房间数量 - 总和(预订数量)?你理想的结果表是否包含 room_id, qty left?
猜你喜欢
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多