gpt4 book ai didi

mysql - 我想合并两个表以获得结果

转载 作者:行者123 更新时间:2023-11-29 06:16:43 25 4
gpt4 key购买 nike

我有两个表,

1.订单,2.项目。

1.订单骨架

+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+

2.元素骨架

+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| order_id | int(11) | NO | MUL | NULL | |
| name | varchar(50) | NO | | NULL | |
| price | int(11) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+

记录:

1.带数据的订单表

+----+--------+
| id | name |
+----+--------+
| 1 | Order1 |
| 2 | Order2 |
| 3 | Order3 |
+----+--------+

2.带数据的Items表

+----+----------+---------+-------+
| id | order_id | name | price |
+----+----------+---------+-------+
| 1 | 1 | Mobile1 | 25000 |
| 2 | 1 | Mobile2 | 30000 |
| 3 | 1 | Mobile3 | 6500 |
| 4 | 2 | Mobile4 | 10000 |
+----+----------+---------+-------+

我想获取具有相同 order_id 的商品,

 Select id[Orders], order_id[items], name[items]1, name[items]2, name[items]3... 

建议请...

最佳答案

看来你只需要访问表items就可以得到你需要的东西。

SELECT order_id, GROUP_CONCAT( name SEPARATOR ', ') item_names
FROM items
GROUP BY order_id;

关于mysql - 我想合并两个表以获得结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com