我有一個MySQL數據庫,包含表'人員','問題'和'答案'。我試圖得到一些易讀的結果。 我有這個疑問:我可以對這些MySQL查詢做些什麼小的調整來獲得我需要的結果?
SELECT CONCAT(people.firstName, ' ', people.lastName) as Name,
people.emailAddress,
(CASE questions.fruit
WHEN 'apple' THEN 'YES'
ELSE ''
END) as Apple,
(CASE questions.fruit
WHEN 'orange' THEN 'YES'
ELSE ''
END) as Orange,
(CASE questions.fruit
WHEN 'banana' THEN 'YES'
ELSE ''
END) as Banana
From answers
Join questions ON answers.fruitID = questions.fruitID
JOIN people ON questions.person = people.person
WHERE questions.questionnaire = '24'
ORDER BY people.person
將會產生這樣的結果:
+------------------+--------------------+-------+--------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+-------+--------+--------+
| Harry Potter | [email protected] | YES | | |
| Harry Potter | [email protected] | YES | | |
| Harry Potter | [email protected] | YES | | |
| Ron Weasley | [email protected] | YES | | |
| Hermione Granger | [email protected] | YES | YES | |
| Hermione Granger | [email protected] | YES | | |
| Hermione Granger | [email protected] | 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 | | |
| Oliver Wood | [email protected] | | | YES |
+------------------+--------------------+-------+--------+--------+
這是偉大的。它具有我需要的所有信息,但是,如果每人只有一行,並且他們選擇了所有的果實(即使他們多次回答),那麼這將是理想選擇。我無法弄清楚如何讓團隊做我想做的事。像這樣:
+------------------+--------------------+-------+--------+--------+
| 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 |
+------------------+--------------------+-------+--------+--------+
所以上面的預期結果會忽略同一水果的多個答案。但是,在某些情況下,我想知道是否有人多次用相同的水果回答。所以,我也得到了該查詢:
SELECT CONCAT(people.firstName, ' ', people.lastName) as Name,
people.emailAddress,
SUM(CASE questions.fruit
WHEN 'apple' THEN 1
ELSE 0
END) as 'AppleCount',
SUM(CASE questions.fruit
WHEN 'orange' THEN 1
ELSE 0
END) as 'OrangeCount',
SUM(CASE questions.fruit
WHEN 'banana' THEN 1
ELSE 0
END) as 'BananaCount'
From answers
Join questions ON answers.fruitID = questions.fruitID
JOIN people ON questions.person = people.person
WHERE questions.questionnaire = '24'
GROUP BY people.person
ORDER BY people.person
將會產生這樣的:
+------------------+--------------------+---------+---------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+---------+---------+--------+
| Harry Potter | [email protected] | 3 | 0 | 0 |
| Ron Weasley | [email protected] | 1 | 0 | 0 |
| Hermione Granger | [email protected] | 3 | 1 | 0 |
| Severus Snape | [email protected] | 1 | 1 | 0 |
| Remus Lupin | [email protected] | 1 | 0 | 0 |
| Viktor Krum | [email protected] | 1 | 0 | 0 |
| Molly Weasley | [email protected] | 1 | 1 | 0 |
| Oliver Wood | [email protected] | 1 | 0 | 1 |
+------------------+--------------------+---------+---------+--------+
,這也是偉大的,但可能是一對夫婦的改善更好。首先(真正的問題)是,當一個人沒有答案但是他們確實存在於問題表中(因爲我已經發送了問卷給他們),他們完全不顯示上述查詢。我想讓他們在結果中列出,他們只是沒有選擇任何水果。其次,我希望「0」只是空白。所以它會看起來像這樣:
+------------------+--------------------+---------+---------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+---------+---------+--------+
| Harry Potter | [email protected] | 3 | | |
| Ron Weasley | [email protected] | 1 | | |
| Hermione Granger | [email protected] | 3 | 1 | |
| Severus Snape | [email protected] | 1 | 1 | |
| Remus Lupin | [email protected] | 1 | | |
| Viktor Krum | [email protected] | 1 | | |
| Molly Weasley | [email protected] | 1 | 1 | |
| Oliver Wood | [email protected] | 1 | | 1 |
| Argus Filch | [email protected] | | | |
+------------------+--------------------+---------+---------+--------+
有關如何獲得這兩個理想結果的任何想法?
編輯:工作樣本數據...
將連接更改爲左連接 – artm
考慮處理應用程序代碼 – Strawberry
中的數據顯示問題請編輯您的問題並顯示涉及的所有表的示例數據。 –