从单个MySQL查询中获得最大收益

时间:2012-11-13 19:42:07

标签: php mysql

我有一个网站列出了可供出租的公寓。我的MySQL数据库中有几个不同的表。一张表是关于整个公寓楼的基本信息,包括地址,设施,照片,身份证(NID)等。

另一张表列出了每栋楼内可供出租的单位。所以每个公寓楼都有多个平面图,有几个工作室,几个单卧室单元,有时几个两居室单元等。

目前,我查询我的“单位”表并询问给定公寓楼内的所有单位。我的查询类似于SELECT * FROM node_unit WHERE nid='555'

这可能会返回如下内容:

NID    Name       Rent    SqFT    Bedrooms    Bathrooms
555    Unit 1     $500    620      0            1
555    Unit 2     $550    680      0            1
555    Unit 3     $600    820      1            1
555    Unit 4     $650    920      1            1
555    Unit 5     $700    1220     2            1
555    Unit 6     $800    1420     2            2
555    Unit 7     $900    1500     3            2
555    Unit 8     $1100   1620     3            3

等等

我在PHP中做的是使用手风琴将1间卧室组合在一起,将2间卧室组合在一起等。

并非所有公寓都有2间卧室,有些只有1间卧室,所以我需要在我的PHP代码中知道是否应该打印另一个手风琴。

目前我正在对数据库进行多次点击,以确定给定建筑物是否有任何2个卧室单位,如果是,则打印另一行。这栋楼有3间卧室吗?如果是这样打印另一行,但我想停止点击我的数据库。

最后,这是我的问题:

如何存储我的第一个数据库调用的结果,并以某种方式解析数据,并确定给定的NID是否有工作室,1张病床,2张病床等? (我刚开始学习PHP / MySQL)

2 个答案:

答案 0 :(得分:1)

我建议像这样的查询:

SELECT * FROM node_unit WHERE nid='555'
ORDER BY Bedrooms ASC, Bathrooms ASC, Rent ASC

这将返回按卧室,#浴室和租金金额(按此顺序)排序的记录。

从数据库中读取时,您可以轻松地将其存储在多维数组中(假设mysqli$result中存储的结果集一起使用,但其他数据库连接库的概念相同)

$room_array = array();
while ($row = mysqli_fetch_assoc($result)) {
    $room_array[(int)$row['Bedrooms']][] = $row;
}

现在你有了一个多维数组,其中卧室数量作为第一个索引。如果你var_dump它,那么数组可能看起来像这样:

Array (
    [0] => Array (
        [0] => Array (
            'NID' => '555',
            'Name' => 'Unit 1',
            'Rent' => '$500',
            'SqFt' => '620',
            'Bedrooms' => '0',
            'Bathrooms' => '1',
        )
        [1] => Array (
            'NID' => '555',
            'Name' => 'Unit 2',
            'Rent' => '$550',
            'SqFt' => '680',
            'Bedrooms' => '0',
            'Bathrooms' => '1',
        )
    )
    [1] => Array (
        [0] => Array (
            'NID' => '555',
            'Name' => 'Unit 3',
            'Rent' => '$600',
            'SqFt' => '820',
            'Bedrooms' => '1',
            'Bathrooms' => '1',
        )
        [1] => Array (
            'NID' => '555',
            'Name' => 'Unit 4',
            'Rent' => '$650',
            'SqFt' => '920',
            'Bedrooms' => '1',
            'Bathrooms' => '1',
        )
    [2] => Array (
        ...
    )
    [3] => Array (
        ...
    )
)

这使得在外循环中迭代多个卧室值非常容易,这会创建您的手风琴,然后在内循环中迭代各个房间。

foreach ($room_array as $num_bedrooms => $rooms) {
    // start accordion
    foreach ($rooms as $room) {
        // output room details
    }
    // end accordion
}

所有这些只需要单一查询。

另外,请确保您在卧室,浴室,租赁(或您在排序中使用的任何一个)以及nid上都有索引,因为它用作过滤器。

答案 1 :(得分:1)

尝试将此作为您的数据库查询,它将为每个建筑物为其拥有的每种单位类型生成一行。您还可以将其用作子查询并加入父表,以便一次性获取有关建筑物的任何其他详细信息。

select 'studio' as unit_type, nid from node_unit group by nid having count(case when bedrooms = 0 then 1 end) > 0
union all
select 'one bedroom' as unit_type, nid from node_unit group by nid having count(case when bedrooms = 1 then 1 end) > 0
union all
select 'two bedroom' as unit_type, nid from node_unit group by nid having count(case when bedrooms = 2 then 1 end) > 0
union all
select 'three bedroom' as unit_type, nid from node_unit group by nid having count(case when bedrooms = 3 then 1 end) > 0

相关问题