0
我有一個MySQL數據庫,其中包含表'people','questions'和'answers'。我有這個疑問:需要MySQL查詢以提供更多可讀結果
SELECT CONCAT(people.firstName, ' ', people.lastName) as Name, people.emailAddress, questions.fruit, answers.fruitID, answers.info, answers.dateLogged, questions.fruitID, questions.questionairID, questions.person
From answers
Join questions ON answers.fruitID= questions.fruitID
JOIN people ON questions.person= people.personID
WHERE questions.questionairID= '24'
ORDER BY people.personID
將會產生這樣的結果:
+------------------+--------------------+--------+-----------------+-------------------+-------------------+-----------------+---------------+--------+
| Name | emailAddress | fruit | fruitInstanceID | info | dateLogged | fruitInstanceID | questionairID | person |
+------------------+--------------------+--------+-----------------+-------------------+-------------------+-----------------+---------------+--------+
| Harry Potter | [email protected] | apple | 107 | NULL | 05/26/17 07:23 PM | 107 | 24 | 6 |
| Ron Weasley | [email protected] | apple | 113 | NULL | 05/26/17 06:30 PM | 113 | 24 | 8 |
| Hermione Granger | [email protected] | apple | 116 | NULL | 05/26/17 06:29 PM | 116 | 24 | 9 |
| Hermione Granger | [email protected] | orange | 114 | NULL | 05/26/17 06:36 PM | 114 | 24 | 9 |
| Hermione Granger | [email protected] | apple | 116 | NULL | 05/26/17 08:28 PM | 116 | 24 | 9 |
| Hermione Granger | [email protected] | orange | 114 | NULL | 05/26/17 08:28 PM | 114 | 24 | 9 |
| Hermione Granger | [email protected] | orange | 114 | some extra info | 05/26/17 08:29 PM | 114 | 24 | 9 |
| Hermione Granger | [email protected] | orange | 114 | more extra info | 05/26/17 08:29 PM | 114 | 24 | 9 |
| Hermione Granger | [email protected] | orange | 114 | NULL | 05/26/17 08:31 PM | 114 | 24 | 9 |
| Severus Snape | [email protected] | apple | 119 | NULL | 05/26/17 06:37 PM | 119 | 24 | 10 |
| Severus Snape | [email protected] | orange | 117 | NULL | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | [email protected] | orange | 117 | user entered info | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | [email protected] | orange | 117 | more user info | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | [email protected] | orange | 117 | info again | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Remus Lupin | [email protected] | apple | 122 | NULL | 05/26/17 06:29 PM | 122 | 24 | 11 |
| Viktor Krum | [email protected] | apple | 125 | NULL | 05/26/17 06:30 PM | 125 | 24 | 12 |
| Molly Weasley | [email protected] | apple | 128 | NULL | 05/26/17 06:36 PM | 128 | 24 | 13 |
| Molly Weasley | [email protected] | apple | 128 | NULL | 05/26/17 07:22 PM | 128 | 24 | 13 |
| Molly Weasley | [email protected] | orange | 126 | NULL | 05/26/17 07:37 PM | 126 | 24 | 13 |
| Molly Weasley | [email protected] | orange | 126 | NULL | 05/26/17 07:37 PM | 126 | 24 | 13 |
| Oliver Wood | [email protected] | apple | 131 | NULL | 05/26/17 06:33 PM | 131 | 24 | 14 |
| Oliver Wood | [email protected] | banana | 133 | NULL | 05/26/17 06:33 PM | 133 | 24 | 14 |
+------------------+--------------------+--------+-----------------+-------------------+-------------------+-----------------+---------------+--------+
這是偉大的。它具有我需要的所有信息(以及更多),但更具可讀性的格式會很好。我可以使用什麼樣的查詢,讓我更多的東西是這樣的:
+------------------+--------------------+-------+--------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+-------+--------+--------+
| Harry Potter | [email protected] | YES | | |
| Ron Weasley | [email protected] | YES | | |
| Hermione Granger | [email protected] | YES | YES | |
| Severus Snape | [email protected] | YES | YES | |
| Remus Lupin | [email protected] | YES | | |
| Viktor Krum | [email protected] | YES | | |
| Molly Weasley | [email protected] | YES | YES | |
| Oliver Wood | [email protected] | YES | | YES |
+------------------+--------------------+-------+--------+--------+
或紅利,也算次數的水果:
+------------------+--------------------+---------+---------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+---------+---------+--------+
| Harry Potter | [email protected] | YES | | |
| Ron Weasley | [email protected] | YES | | |
| Hermione Granger | [email protected] | YES (2) | YES (5) | |
| Severus Snape | [email protected] | YES | YES (4) | |
| Remus Lupin | [email protected] | YES | | |
| Viktor Krum | [email protected] | YES | | |
| Molly Weasley | [email protected] | YES (2) | YES (2) | |
| Oliver Wood | [email protected] | YES | | YES |
+------------------+--------------------+---------+---------+--------+
我將在幾分鐘後重試。我忘記更新查詢中的一些字段以查看我的示例。這就是爲什麼你看到了employeeid而不是personid,這在問題中更有意義。 。 –
這工作得很好!第一個結果列出了所有人的記錄(所以我爲Hermione和其他人記錄了幾行),但我認爲我可以通過一個組來解決這個問題。第二個工作完美。謝謝! –
太棒了! Enjoy ... –