2012-09-18 58 views
0

我有一個MySQL數據庫與幾個表。他們是這個樣子 -如何在查詢FNF後的兩個表時獲得一行結果?

食物表:

+----------+------------+--------------+ 
| 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。在此先感謝您的幫助。

+0

你想要得到單行上的所有四個好記錄,這是你的問題嗎? –

+0

@MoyedAnsari是的...... –

回答

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

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

我會看到,如果我可以重新用於演示


DEMO:

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

這是完美的,正是我所期待的,謝謝:)是否可以命名錶頭「food」而不是'GROUP_CONCAT(food SEPARATOR',')'? –

+0

@nitstorm爲group_concat添加別名 –

+0

@MoyedAnsari謝謝:) Mike S .:感謝您的回答,編輯它以添加別名並接受它 –

相關問題