我有一个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 | hpotter@gmail.com | apple | 107 | NULL | 05/26/17 07:23 PM | 107 | 24 | 6 |
| Ron Weasley | rweasley@gmail.com | apple | 113 | NULL | 05/26/17 06:30 PM | 113 | 24 | 8 |
| Hermione Granger | hgranger@gmail.com | apple | 116 | NULL | 05/26/17 06:29 PM | 116 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | orange | 114 | NULL | 05/26/17 06:36 PM | 114 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | apple | 116 | NULL | 05/26/17 08:28 PM | 116 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | orange | 114 | NULL | 05/26/17 08:28 PM | 114 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | orange | 114 | some extra info | 05/26/17 08:29 PM | 114 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | orange | 114 | more extra info | 05/26/17 08:29 PM | 114 | 24 | 9 |
| Hermione Granger | hgranger@gmail.com | orange | 114 | NULL | 05/26/17 08:31 PM | 114 | 24 | 9 |
| Severus Snape | ssnape@gmail.com | apple | 119 | NULL | 05/26/17 06:37 PM | 119 | 24 | 10 |
| Severus Snape | ssnape@gmail.com | orange | 117 | NULL | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | ssnape@gmail.com | orange | 117 | user entered info | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | ssnape@gmail.com | orange | 117 | more user info | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Severus Snape | ssnape@gmail.com | orange | 117 | info again | 05/26/17 06:46 PM | 117 | 24 | 10 |
| Remus Lupin | rlupin@gmail.com | apple | 122 | NULL | 05/26/17 06:29 PM | 122 | 24 | 11 |
| Viktor Krum | vkrum@gmail.com | apple | 125 | NULL | 05/26/17 06:30 PM | 125 | 24 | 12 |
| Molly Weasley | mweasley@gmail.com | apple | 128 | NULL | 05/26/17 06:36 PM | 128 | 24 | 13 |
| Molly Weasley | mweasley@gmail.com | apple | 128 | NULL | 05/26/17 07:22 PM | 128 | 24 | 13 |
| Molly Weasley | mweasley@gmail.com | orange | 126 | NULL | 05/26/17 07:37 PM | 126 | 24 | 13 |
| Molly Weasley | mweasley@gmail.com | orange | 126 | NULL | 05/26/17 07:37 PM | 126 | 24 | 13 |
| Oliver Wood | owood@gmail.com | apple | 131 | NULL | 05/26/17 06:33 PM | 131 | 24 | 14 |
| Oliver Wood | owood@gmail.com | banana | 133 | NULL | 05/26/17 06:33 PM | 133 | 24 | 14 |
+------------------+--------------------+--------+-----------------+-------------------+-------------------+-----------------+---------------+--------+
这很棒。它具有我需要的所有信息(以及更多),但更可读的格式会很好。我可以使用什么查询来获得更像这样的内容:
+------------------+--------------------+-------+--------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+-------+--------+--------+
| Harry Potter | hpotter@gmail.com | YES | | |
| Ron Weasley | rweasley@gmail.com | YES | | |
| Hermione Granger | hgranger@gmail.com | YES | YES | |
| Severus Snape | ssnape@gmail.com | YES | YES | |
| Remus Lupin | rlupin@gmail.com | YES | | |
| Viktor Krum | vkrum@gmail.com | YES | | |
| Molly Weasley | mweasley@gmail.com | YES | YES | |
| Oliver Wood | owood@gmail.com | YES | | YES |
+------------------+--------------------+-------+--------+--------+
或奖金,也计算水果的次数:
+------------------+--------------------+---------+---------+--------+
| Name | emailAddress | Apple | Orange | Banana |
+------------------+--------------------+---------+---------+--------+
| Harry Potter | hpotter@gmail.com | YES | | |
| Ron Weasley | rweasley@gmail.com | YES | | |
| Hermione Granger | hgranger@gmail.com | YES (2) | YES (5) | |
| Severus Snape | ssnape@gmail.com | YES | YES (4) | |
| Remus Lupin | rlupin@gmail.com | YES | | |
| Viktor Krum | vkrum@gmail.com | YES | | |
| Molly Weasley | mweasley@gmail.com | YES (2) | YES (2) | |
| Oliver Wood | owood@gmail.com | YES | | YES |
+------------------+--------------------+---------+---------+--------+
答案 0 :(得分:0)
这适用于第一种情况 -
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',
(CASE questions.fruit
WHEN 'banana' THEN 1
ELSE 0
END) as 'BananaCount'
From answers
Join questions ON answers.attackID = campaign.attackID
JOIN employees ON campaign.employee = employees.employeeID
WHERE campaign.campaignID = '24'
GROUP BY employees.employeeID
ORDER BY employees.employeeID
不确定在第二种情况下计数是如何工作的。但我认为这将是对雇员的分组。
<强>更新强>
对于第二种情况,我认为不需要文字“是”,水果的数量应足以决定。试试这个算 -
decorators