mysql - 根据id从多个表中检索数据

标签 mysql inner-join

使用 php,我试图从 2 个不同的表获取数据:

Table : products

+-------+---------+----------+
| pcode | product | category |
+-------+---------+----------+
| 1     | jeans   | men      |
+-------+---------+----------+
| 2     | shirt   | men      |
+-------+---------+----------+


Table : colors
+-------+---------+
| pcode | colors  | 
+-------+---------+
| 1     | blue    |
+-------+---------+
| 1     | black   |
+-------+---------+
| 1     | white   |
+-------+---------+
| 2     | yellow  |
+-------+---------+

Result I want should be like :

+-------+---------+----------+---------------------+
| pcode | product | category | colors              |
+-------+---------+----------+---------------------+
| 1     | jeans   | men      | black, blue, white  |
+-------+---------+----------+---------------------+
| 2     | shirt   | men      | yellow              |
+-------+---------+----------+---------------------+

请指导我。

提前致谢。

维克拉姆

最佳答案

SELECT p.pcode,product,category,GROUP_CONCAT(colors) as colors
FROM products p JOIN colors c 
ON p.pcode=c.pcode 
GROUP BY product

关于mysql - 根据id从多个表中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20583748/

相关文章:

使用映射表的 SQL JOIN

mysql - 数据库多表更新同时永久访问

mysql - 在 mysql 子查询中使用外部别名

mysql - 如何设置mysql错误日志来捕获mysql语法错误

mysql - 与 MySQL 中不同数据库中的另一个表相比,我如何从一个表中获取缺失的行?

MySql,内连接查询,必须匹配多行

mysql - Negative INNER JOIN [没有 LEFT JOIN 且 IS NULL]

MySql 2个表的内连接

mysql - SQL触发器用于将值删除到表中并将删除的值更新到另一个表中

mysql - IF/case如何选择inner join field?