0

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

我有 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 ”、“数量”和“状态”。

在我的表保留中,每个id都有不同的* qty *,所以我想要每个idqty总和,如下所示:

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


$total = sum(qty); which is 11

你们能帮我如何创建一个查询来获取每个id的这些qty的总和吗?还请检查前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 

所以在我的显示中,我有一个来自桌子房间的选择标签(id = 11),选项是数量,并从桌子保存(room_id = 11 扣除数量

4

1 回答 1

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 中不需要任何逻辑。

于 2012-11-02T02:07:34.583 回答