2017-01-17 51 views
3

我从那里我试图获取数据 表3个MySQL表:表PHP SQL左连接获取所有

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 

只有单一类别名称是否可以在单个查询?

+0

这只是一个简单的循环(在一个有序的结果)在PHP中 - 而你的断言是不正确的。 – Strawberry

+0

另外请注意,list_to_category中的代理键没有任何作用。 – Strawberry

+0

我真的不会为此使用GROUP_CONCAT。这是不必要的限制。 – Strawberry

回答

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 
+0

嘿!感谢您的回答。它解决了我的问题:) – Sanjeev

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 
+0

然后根据“名称”按“list_id” – jainvikram444

2

你可以试试这个解决方案。

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 

希望这会帮助你!

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 
         ) 

       ) 

     ) 

) 
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