获取MYSQL结果而不重复重复

时间:2015-04-30 14:10:36

标签: php mysql

我很难从我的数据库中获取结果。我有一张表格列出了汽车品牌的表格,另一张表格按型号列出了汽车,另一张表格列出了哪个型号属于哪个品牌。

以下是我的表格:

品牌:

+--------------+----------------+
| car_brand_id | car_brand_name |
+--------------+----------------+
| 1            | acura          |
+--------------+----------------+
| 2            | honda          |
+--------------+----------------+
| 3            | toyota         |
+--------------+----------------+
| 4            | audi           |
+--------------+----------------+

型号:

+--------------+----------------+
| car_model_id | car_model_name |
+--------------+----------------+
| 1            | sienna         |
+--------------+----------------+
| 2            | corolla        |
+--------------+----------------+
| 3            | mdx            |
+--------------+----------------+
| 4            | accord         |
+--------------+----------------+
| 5            | a4             |
+--------------+----------------+

作业:

+-----------+-----------------+-----------------+
| assign_id | car_brand_fk_id | car_model_fk_id |
+-----------+-----------------+-----------------+
| 1         | 1               | 3               |
+-----------+-----------------+-----------------+
| 2         | 2               | 4               |
+-----------+-----------------+-----------------+
| 3         | 3               | 1               |
+-----------+-----------------+-----------------+
| 4         | 3               | 2               |
+-----------+-----------------+-----------------+
| 5         | 4               | 5               |
+-----------+-----------------+-----------------+

目前我正在做以下事情:

SELECT brands.car_brand_id, brands.car_brand_name, models.car_model_name
FROM brands
LEFT JOIN assignments ON assignments.car_brand_fk_id=brands.car_brand_id 
LEFT JOIN models ON models.car_model_id=assignments.car_model_fk_id
ORDER BY brands.car_brand_name

这些是我的结果:

+--------------+----------------+----------------+
| car_brand_id | car_brand_name | car_model_name |
+--------------+----------------+----------------+
| 1            | acura          | mdx            |
+--------------+----------------+----------------+
| 2            | honda          | accord         |
+--------------+----------------+----------------+
| 3            | toyota         | sienna         |
+--------------+----------------+----------------+
| 3            | toyota         | corolla        |
+--------------+----------------+----------------+
| 4            | audi           | a4             |
+--------------+----------------+----------------+

正如你所看到的,其中一些正在重复(丰田)。我想要的是以下内容:

+--------------+----------------+-----------------+
| car_brand_id | car_brand_name | car_model_name  |
+--------------+----------------+-----------------+
| 1            | acura          | mdx             |
+--------------+----------------+-----------------+
| 2            | honda          | accord          |
+--------------+----------------+-----------------+
| 3            | toyota         | sienna, corolla |
+--------------+----------------+-----------------+
| 4            | audi           | a4              |
+--------------+----------------+-----------------+

我该怎么做?

4 个答案:

答案 0 :(得分:1)

这可以通过分组和使用group_concat

来完成
SELECT brands.car_brand_id, brands.car_brand_name, group_concat(models.car_model_name separator ', ')
FROM brands
LEFT JOIN assignments ON assignments.car_brand_fk_id=brands.car_brand_id 
LEFT JOIN models ON models.car_model_id=assignments.car_model_fk_id
GROUP BY brands.car_brand_name
ORDER BY brands.car_brand_name

答案 1 :(得分:1)

尝试使用group by和group_concat函数,如:

SELECT brands.car_brand_id, brands.car_brand_name, GROUP_CONCAT(models.car_model_name) AS car_model_name
FROM brands
LEFT JOIN assignments ON assignments.car_brand_fk_id=brands.car_brand_id 
LEFT JOIN models ON models.car_model_id=assignments.car_model_fk_id
GROUP BY brands.car_brand_id, brands.car_brand_name
ORDER BY brands.car_brand_name

答案 2 :(得分:0)

您需要GROUP BYGROUP_CONCAT()的组合:

SELECT brands.car_brand_id, brands.car_brand_name, GROUP_CONCAT(models.car_model_name)
FROM brands
LEFT JOIN assignments ON assignments.car_brand_fk_id=brands.car_brand_id 
LEFT JOIN models ON models.car_model_id=assignments.car_model_fk_id
GROUP BY models.car_model_name
ORDER BY brands.car_brand_name

答案 3 :(得分:-1)

你可以使用......

select distinct(COLUMN NAME,COLUMN NAME,COLUMN NAME);