预订日期的酒店预订查询

时间:2012-11-02 01:54:37

标签: php mysql sql

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

我有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 ','到达','离开',' room_id < / strong>','数量'和'状态'。

在我的表格中保留每个 ID 不同 * qty *,所以我想总和 数量数量数量数据

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


$total = sum(qty); which is 11

你们可以帮助我如何创建一个查询来获取每个 qty 总和 ID 的?另请检查第一个 3 查询,因为我知道每个查询都有错误。获取 array_merge array_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 

所以在我的展示中,我有一个来自桌面房间(id = 11)的精选标签,选项是数量扣除 <表保存(room_id = 11)

数量的强>总和

1 个答案:

答案 0 :(得分:0)

使用一个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中使用任何逻辑。