PHP SQL左连接获取全部

时间:2017-01-17 10:55:51

标签: php mysql

我有3个mysql表,我试图获取数据 表:列表

list_id  |  name  |  description
-------------------------------------
1234     |  name1 | sample description1
1235     |  name2 | sample description2

表:list_to_category

id     |  list_id  | category_id
--------------------------------
1      |  1234     | 1
2      |  1234     | 2
3      |  1234     | 3
4      |  1235     | 2
5      |  1235     | 3

表格:类别

id     |  title      | parent_id
--------------------------------
1      |  Category 1 | 0
2      |  Category 2 | 0
3      |  Category 3 | 0

从PHP SQL查询中我想获取如下所示的数据

1. name1 - category 1, category 2, category 3
2. name2 - category 2, category 3

我在下面尝试了查询

SELECT list.name, category.title FROM list

        LEFT JOIN list_to_category
        ON list.id = list_to_category.list_id

        LEFT JOIN category
        ON list_to_category.id = category.id

这只给我一个类别名称,分配给像这样的列表

1. name1 - category 1
2. name2 - category 2

单个查询是否可以?

5 个答案:

答案 0 :(得分:2)

您可以使用GROUP_CONCAT:

select
    l.list_id,
    l.name,
    group_concat(distinct c.title) categories
from list l
left join list_to_category lc
on l.list_id = lc.list_id
left join category c
on lc.category_id = c.id
group by l.list_id

答案 1 :(得分:1)

将GROUP_CONCAT用于"名称"获取结果:

SELECT L.name, GROUP_CONCAT(C.title) as title  FROM list L    
        LEFT outer  JOIN list_to_category LC  ON L.list_id = LC.list_id    
        LEFT outer JOIN category C  ON LC.category_id = C.id
        group by L.name

将GROUP_CONCAT用于" list_id"获取结果的同名列表:

SELECT L.name, GROUP_CONCAT(C.title) as title  FROM list L    
            LEFT outer  JOIN list_to_category LC  ON L.list_id = LC.list_id    
            LEFT outer JOIN category C  ON LC.category_id = C.id
            group by L.list_id

答案 2 :(得分:1)

您可以尝试此解决方案。

select l.list_id, l.name, (select group_concat(c.title) from list_to_category ltc JOIN category c ON c.id=ltc.category_id where ltc.list_id=l.id) from list l

希望这会对你有帮助!!!

答案 3 :(得分:0)

从下面的代码中可以明显看出,我没有PHP编码器。但是,这应该可以解决这个问题。您还可以使用javascript / css来处理转换,这意味着事情可能更加动态......

哦,我更改了一些表/列名称 - 因为我更喜欢它...

<?php

require('path/to/connection/statements'); // $con 

$query = "
SELECT l.list_id
     , l.name
     , l.description
     , c.category_id
     , c.title
     , c.parent_id
  FROM list l
  JOIN list_category lc
    ON lc.list_id = l.list_id
  JOIN category c
    ON c.category_id = lc.category_id
 ORDER
    BY l.list_id
     , c.category_id;
";

$result = mysqli_query($con,$query);

$my_array = array();

while($row = mysqli_fetch_assoc($result)){
$my_array[] = $row;
}

$new_array = array();
foreach ($my_array as $row)
{
   $new_array[$row['list_id']][$row['name']][$row['description']][] = $row['title'];
}

print_r($new_array);

?>

这会变成这样的数组:

Array
(
    [0] => Array
        (
            [list_id] => 1234
            [name] => name1
            [description] => sample description1
            [category_id] => 1
            [title] => Category 1
            [parent_id] => 0
        )

    [1] => Array
        (
            [list_id] => 1234
            [name] => name1
            [description] => sample description1
            [category_id] => 2
            [title] => Category 2
            [parent_id] => 0
        )

    [2] => Array
        (
            [list_id] => 1234
            [name] => name1
            [description] => sample description1
            [category_id] => 3
            [title] => Category 3
            [parent_id] => 0
        )

    [3] => Array
        (
            [list_id] => 1235
            [name] => name2
            [description] => sample description2
            [category_id] => 2
            [title] => Category 2
            [parent_id] => 0
        )

    [4] => Array
        (
            [list_id] => 1235
            [name] => name2
            [description] => sample description2
            [category_id] => 3
            [title] => Category 3
            [parent_id] => 0
        )

)

...进入像这样的数组......

Array
(
    [1234] => Array
        (
            [name1] => Array
                (
                    [sample description1] => Array
                        (
                            [0] => Category 1
                            [1] => Category 2
                            [2] => Category 3
                        )

                )

        )

    [1235] => Array
        (
            [name2] => Array
                (
                    [sample description2] => Array
                        (
                            [0] => Category 2
                            [1] => Category 3
                        )

                )

        )

)

答案 4 :(得分:0)

试试这段代码

select l.name,c.title   
    from list_to_category lc join list l on lc.list_id=l.id 
    join category c on lc.catg_id=c.id