MYSQL:两行结果排成一行

时间:2014-08-20 10:36:48

标签: php mysql

我解决了一个问题。我有两张桌子: 订单 - 定义客户订单

+-------------------+-------------+------+-----+-------------------+----------------+
| Field             | Type        | Null | Key | Default           | Extra          |
+-------------------+-------------+------+-----+-------------------+----------------+
| id                | int(11)     | NO   | PRI | NULL              | auto_increment |
| customer_id       | int(11)     | NO   | MUL | NULL              |                |
| from_date         | date        | YES  |     | NULL              |                |
| datum_dodani      | date        | NO   |     | NULL              |                |
| popis             | text        | NO   |     | NULL              |                |
| status            | tinyint(4)  | NO   |     | 1                 |                |
| cena              | varchar(8)  | NO   |     | NULL              |                |
| vytvoril          | tinyint(4)  | NO   | MUL | NULL              |                |
| vytvoreno         | timestamp   | NO   |     | CURRENT_TIMESTAMP |                |
| vyresil           | tinyint(4)  | YES  |     | NULL              |                |
| prirazena_skupina | tinyint(4)  | YES  |     | NULL              |                |
| narocnost         | smallint(6) | YES  |     | NULL              |                |
| cas               | varchar(6)  | YES  |     | NULL              |                |
| priorita          | tinyint(1)  | NO   |     | 0                 |                |
+-------------------+-------------+------+-----+-------------------+----------------+

和POLOZKY_OBJEDNAVKY定义了每一行客户订单:

+-----------+------------+------+-----+---------+----------------+
| Field     | Type       | Null | Key | Default | Extra          |
+-----------+------------+------+-----+---------+----------------+
| id        | int(11)    | NO   | PRI | NULL    | auto_increment |
| order_id  | int(11)    | YES  | MUL | NULL    |                |
| servis_id | tinyint(4) | YES  | MUL | NULL    |                |
| cena      | int(11)    | YES  |     | NULL    |                |
| pocet     | tinyint(4) | YES  |     | NULL    |                |
| popis     | text       | YES  |     | NULL    |                |
| bike      | text       | YES  |     | NULL    |                |
+-----------+------------+------+-----+---------+----------------+

这两个表通过polozky_objednavky.order_id链接 - > orders.id

问题是我无法创建会返回类似内容的查询:

+-------------------+-------------+--------------+------------------------+
| orders.id         | customer_id | orders.status| polozky_objednavky.bike|
+-------------------+-------------+--------------+------------------------+
| 1                 | 23          |             2|              24, 25, 46|
| 2                 | 25          |             5|                      36|

所以这意味着在polozky_objednavky.bike列中会有一个select,它返回POLOZKY_OBJEDNAVKY中的3行,键为POLOZKY_OBJEDNAVKY.ORDER_ID = ORDERS.ID

希望我能很好地解释并抱歉愚蠢的问题,但我试图用JOINS选择它或从select中选择但没有成功:(

谢谢你, 微米。

2 个答案:

答案 0 :(得分:2)

您需要加入表格。试试这个:

SELECT o.id AS orders_id,
       o.customer_id,
       o.status,
       p.bike
  FROM ORDERS AS o
  LEFT OUTER JOIN POLOZKY_OBJEDNAVKY AS p
    ON p.order_id = o.id

如果您有特定条件,只需将其作为WHERE子句添加到最后。

您可以在w3schoolsitepoint

上阅读有关联接的信息

答案 1 :(得分:0)

我认为您需要使用group_concat()进行汇总查询:

SELECT o.id AS orders_id, o.customer_id, o.status,
       GROUP_CONCAT(p.bike SEPARATOR ', ') as bikes
FROM ORDERS o JOIN
     POLOZKY_OBJEDNAVKY po
     ON p.order_id = o.id
GROUP BY o.id, po.customer_id;