我想结合两个表来获得结果

时间:2016-02-18 08:09:26

标签: mysql join

我有两张桌子,

1.Orders, 2.Items。

1.Orders Skeleton

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

2.Items Skeleton

+----------+-------------+------+-----+---------+----------------+
| 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 Table with Data

+----+----------+---------+-------+
| 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... 

建议请......

2 个答案:

答案 0 :(得分:2)

您似乎只需要访问表items即可获得所需内容。

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

答案 1 :(得分:0)

我只获得已知数量的字段的解决方案....

行到列:

STEP:1

select @orderID:=order_id, 
@name:=GROUP_CONCAT(name) item_names, 
@price:=GROUP_CONCAT(price) item_price 
from item group by order_id limit 1;

输出:

+----------+-------------------------+------------------+
| order_id | item_names              | item_price       |
+----------+-------------------------+------------------+
|        1 | Mobile1,Mobile2,Mobile3 | 25000,30000,6500 |
+----------+-------------------------+------------------+

STEP:2

select @orderID,
SUBSTRING_INDEX(SUBSTRING_INDEX(@name, ',', 1),',',-1) as Name1,
SUBSTRING_INDEX(SUBSTRING_INDEX(@price, ',', 1),',',-1) as Price1, 
SUBSTRING_INDEX(SUBSTRING_INDEX(@name, ',', 2),',',-1) as Name2, 
SUBSTRING_INDEX(SUBSTRING_INDEX(@price, ',', 2),',',-1) as Price2,
SUBSTRING_INDEX(SUBSTRING_INDEX(@name, ',', 3),',',-1) as Name3, 
SUBSTRING_INDEX(SUBSTRING_INDEX(@price, ',', 3),',',-1) as Price3;

输出:

+----------+---------+--------+---------+--------+---------+--------+
| @orderID | Name1   | Price1 | Name2   | Price2 | Name3   | Price3 |
+----------+---------+--------+---------+--------+---------+--------+
|        1 | Mobile1 | 25000  | Mobile2 | 30000  | Mobile3 | 6500   |
+----------+---------+--------+---------+--------+---------+--------+