繁体   English   中英

从表中获取多个值

[英]Get multiple values from table

我有桌子的顾客

| 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个列表

第三个表称为列表

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

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

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

尝试这个:

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM