从表中获取多个值

时间:2013-09-22 18:01:46

标签: php mysql sql

我有一个表客户

| id | firstname | 
| 1  | paul      |
| 2  | steve     |
| 3  | chris     |

第二个名为list_customer的表

| id | id_customer | id_list |
| 1  |     1       |    1    |
| 2  |     1       |    2    |
| 3  |     2       |    1    |

每个客户可以使用x个列表

第三个名为list的表

| id_list | color |
|    1    | #fff  |
|    2    | #000  |
|    3    | #ccc  |

使用mysql查询我想得到名字和列表颜色。 客户可以有多个列表。

2 个答案:

答案 0 :(得分:0)

select c.firstname, group_concat(l.color) as colors
from customers c
inner join list_customer lc on lc.id_customer = c.id
inner join list l on l.id_list = lc.id_list
group by c.firstname

答案 1 :(得分:0)

试试这个:

SELECT c.FIRSTNAME, 
       l.COLOR AS ListColors 
FROM   CUSTOMERS c, 
       LIST_CUSTOMER lc, 
       LIST l 
WHERE  lc.ID_CUSTOMER = c.ID 
       AND l.ID_LIST = lc.ID_LIST 
GROUP  BY c.FIRSTNAME