0

我有一个带有几张表的 MySQL 数据库。它们看起来像这样 -

食物表:

+----------+------------+--------------+
| username | date       | food         |
+----------+------------+--------------+
| test123  | 2012-09-16 | rice         |
| test123  | 2012-09-16 | pizza        |
| test123  | 2012-09-16 | french fries |
| test123  | 2012-09-16 | burger       |
+----------+------------+--------------+

主表:

+----------+------------+----------------+---------------+-------------+-------------+
| username | date       | water_quantity | water_chilled | smoked_what | smoke_count |
+----------+------------+----------------+---------------+-------------+-------------+
| test123  | 2012-09-16 |              1 | no            | cigarettes  |          20 |
+----------+------------+----------------+---------------+-------------+-------------+

当我使用查询SELECT * FROM main,food WHERE main.date=food.date;时,我得到四行结果。我怎么可能在一行中得到结果?最终,当我将结果编码为 JSON 时,我希望它看起来像这样 -

[
   {
      "username":"test123",
      "date":"2012-09-16",
      "water_quantity":"1",
      "water_chilled":"no",
      "smoked_what":"cigarettes",
      "smoke_count":"20",
       {
          "food":"rice",
          "food":"pizza",
          "food":"french fries",
          "food":"burger",                  
       },
   }
]

或类似的东西。我是 MySQL 和数据库的新手,也是 JSON 的新手。在此先感谢您的帮助。

4

1 回答 1

1
select m.*, GROUP_CONCAT(food SEPARATOR ',') AS food FROM main m INNER JOIN food f ON f.username = m.username and f.date = m.date;

当然,您可以更改选择哪些字段来控制输出,但这将解决您的重复问题。

至于结果集中的嵌套食物列表,可以使用 GROUP_CONCAT

参见:http ://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

我会看看我是否可以为演示重新创建


演示:

mysql> create table main (id INT NOT NULL AUTO_INCREMENT, username varchar(12) NOT NULL, date DATETIME, water_quality INT, water_chilled CHAR(3), smoked_what varchar(32), smoke_count INT, primary key (id));
Query OK, 0 rows affected (0.04 sec)

mysql> create table food (id INT NOT NULL AUTO_INCREMENT, username varchar(12) NOT NULL, date DATETIME, food varchar(32), primary key (id));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into food VALUES (1,'test123','2012-09-16','rice'),(2,'test123','2012-09-16','pizza'),(3,'test123','2012-09-16','french fries'),(4,'test123','2012-09-16','burger');Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into main VALUES (1, 'test123', '2012-09-16', 1, 'no', 'cigarettes', 20);
Query OK, 1 row affected (0.00 sec)

mysql> select m.*, GROUP_CONCAT(food SEPARATOR ',') AS food FROM main m INNER JOIN food f ON f.username = m.username and f.date = m.date;
+----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
| id | username | date                | water_quality | water_chilled | smoked_what | smoke_count | food |
+----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
|  1 | test123  | 2012-09-16 00:00:00 |             1 | no            | cigarettes  |          20 | rice,pizza,french fries,burger   |
+----+----------+---------------------+---------------+---------------+-------------+-------------+----------------------------------+
1 row in set (0.00 sec)

mysql> 
于 2012-09-18T17:33:50.337 回答