如何通过连接查询从三个表中选择所有匹配的行

时间:2014-11-02 13:06:52

标签: php mysql arrays

我需要通过连接三个mysql表来创建一个垂直菜单。

这是普通菜单应该的。

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
                    Categories 1 --> Sub Cat Items 2
                    Categories 2 --> Sub Cat Items 3
                    Categories 3 --> Sub Cat Items 4

Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5

这是我拥有的三张桌子 表:Main_cat

+-------------+---------------+
| main_cat_id | main_cat_name |
+-------------+---------------+
|           1 | main cat 1    |
|           2 | main cat 2    |
|           3 | main cat 3    |
+-------------+---------------+

表:类别

+--------+-------------+----------------+
| cat_id | main_cat_id | cat_name       |
+--------+-------------+----------------+
|      1 |           1 | cat1 parent 1  |
|      4 |           1 | cat2 parent 1  |
|      5 |           1 | cat3 parent 1  |
|      6 |           2 | cat 4 parent 2 |
|      7 |           2 | cat 5 parent 2 |
|      8 |           3 | cat 6 parent 3 |
+--------+-------------+----------------+

表:Sub_cat

+------------+-------------+--------+----------------------+
| sub_cat_id | main_cat_id | cat_id | sub_cat_name         |
+------------+-------------+--------+----------------------+
|          1 |           1 |      1 | sub 1 cat1 parent 1  |
|          2 |           1 |      1 | sub 2 cat1 parent 1  |
|          3 |           1 |      4 | sub 3 cat 4 parent 1 |
+------------+-------------+--------+----------------------+

每次我尝试时,我只得到一个包含某些字段的数组,但我期待的是一个malty维数组。这些是我试过的查询。

这是我通过以下查询之一获得的数组之一。

Array
(
    [0] => stdClass Object
        (
            [main_cat_name] => main cat 1
            [main_cat_id] => 1
            [main_cat_url] => 
            [cat_id] => 1
            [cat_name] => cat1 parent 1
            [cat_url] => 
            [sub_cat_id] => 1
            [sub_cat_name] => sub 1 cat1 parent 1
            [sub_cat_url] => 
        )

    [1] => stdClass Object
        (
            [main_cat_name] => main cat 2
            [main_cat_id] => 2
            [main_cat_url] => 
            [cat_id] => 
            [cat_name] => 
            [cat_url] => 
            [sub_cat_id] => 
            [sub_cat_name] => 
            [sub_cat_url] => 
        )

    [2] => stdClass Object
        (
            [main_cat_name] => main cat 3
            [main_cat_id] => 3
            [main_cat_url] => 
            [cat_id] => 
            [cat_name] => 
            [cat_url] => 
            [sub_cat_id] => 
            [sub_cat_name] => 
            [sub_cat_url] => 
        )

)



$query = "SELECT "
                . "mc.main_cat_name, mc.main_cat_url, cg.cat_name, cg.cat_url,                      sc.sub_cat_name, sc.sub_cat_url "
                . "FROM"
                . " main_cat AS mc "
                . "LEFT JOIN "
                . "categories AS cg "
                . "ON "
                . "mc.main_cat_id = cg.main_cat_id "
                . "LEFT JOIN "
                . "sub_cat AS sc "
                . "ON "
                . "cg.cat_id = sc.sub_cat_id ";

$query = "SELECT "
                . "mc.main_cat_name, mc.main_cat_id, mc.main_cat_url, cg.cat_id, cg.cat_name, cg.cat_url, sc.sub_cat_id, sc.sub_cat_name, sc.sub_cat_url "
                . "FROM"
                . " main_cat AS mc "
                . "LEFT JOIN "
                . "sub_cat AS sc "
                . "ON "
                . "sc.main_cat_id = mc.main_cat_id "
                . "LEFT JOIN "
                . "categories AS cg "
                . "ON "
                . "cg.cat_id = sc.cat_id "
                . "GROUP BY "
                . "main_cat_id";



$query = "SELECT "
            . "* "
            . "FROM "
            . "main_cat mc "
            . "LEFT JOIN sub_cat sc "
            . "ON sc.main_cat_id = mc.main_cat_id "
            . "LEFT JOIN categories cg "
            . "ON "
            . "cg.cat_id = sc.cat_id "
            . "Group BY "
            . "sc.cat_id, sc.main_cat_id";

但是这些查询没有给我我期待的东西。你可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

最接近的查询是:

select mc.main_cat_name, c.cat_name, sc.sub_cat_name
from main_cat mc left join
     categories c
     on c.main_cat_id = mc.main_cat_id left join
     sub_cat sc
     on sc.cat_id = c.cat_id 
order by mc.main_cat_id, c.cat_id, sc.sub_cat_id;

但是,这将为您填写每个单元格的“关系”结果。更像是:

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 2
Main Cat Items 1 -> Categories 2 --> Sub Cat Items 3
Main Cat Items 1 -> Categories 3 --> Sub Cat Items 4
Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5

如果要删除重复的名称以进行演示,则应在应用程序中执行此操作。可以在数据库中执行此转换。但是,通常SQL查询的结果集是表,这意味着排序并不重要。在这种情况下,排序很关键,因为否则您将无法知道第二行,第三行和第四行的正确主类别。