2017-05-27 49 views
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 | 
+------------------+--------------------+---------+---------+--------+ 

回答

0

这应该第一种情况下工作 -

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.attackID = campaign.attackID 
JOIN employees ON campaign.employee = employees.employeeID 
WHERE campaign.campaignID = '24' 
ORDER BY employees.employeeID 

不确定在第二种情况下计数如何工作。但我认为它会在employeeid上分组。

UPDATE

因为我不认为有必要对文本第二种情况“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', 
(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 

+0

我将在几分钟后重试。我忘记更新查询中的一些字段以查看我的示例。这就是为什么你看到了employeeid而不是personid,这在问题中更有意义。 。 –

+0

这工作得很好!第一个结果列出了所有人的记录(所以我为Hermione和其他人记录了几行),但我认为我可以通过一个组来解决这个问题。第二个工作完美。谢谢! –

+0

太棒了! Enjoy ... –