我有一个包含以下条目的表格:
+------------+-----------+----------+
| screenId | userInput | numInput |
+------------+-----------+----------+
| 13_1_2_1 | 2 | 9 |
| 13_1_2_2 | 2 | 9 |
| 13_1_2_2 | 3 | 2 |
| 13_1_2_2 | 9 | 2 |
| 13_1_2_2_2 | 3 | 3 |
| 13_1_2_2_2 | 5 | 2 |
| 13_2_2_2 | 4 | 4 |
| 13_2_2_2 | 5 | 4 |
| 13_2_2_2 | 7 | 2 |
+------------+-----------+----------+
我需要构建一个查询,将输出显示为:
+------------+---+---+---+---+---+---+---+---+---+---+
| screenId | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+------------+---+---+---+---+---+---+---+---+---+---+
| 13_1_2_1 | 0 | 0 | 9 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 13_1_2_2 | 0 | 0 | 9 | 2 | 0 | 0 | 0 | 0 | 0 | 2 |
| 13_1_2_2_2 | 0 | 0 | 0 | 3 | 0 | 2 | 0 | 0 | 0 | 0 |
| 13_2_2_2 | 0 | 0 | 0 | 0 | 4 | 4 | 0 | 2 | 0 | 0 |
+------------+---+---+---+---+---+---+---+---+---+---+
这里0-9列值是userInput值,而它们的值是numInput值。例如,第一行表示值userInput'2'的numInput,screenID'13_1_2_1'表示'9'。
我无法为此问题构建正确的查询。请帮忙。
答案 0 :(得分:1)
非常非常糟糕......
SELECT p.screenId, p0.numInput `0`, p1.numInput `1`, p2.numInput `2`, p3.numInput `3`, p4.numInput `4`, p5.numInput `5`, p6.numInput `6`, p7.numInput `7`, p8.numInput `8`, p9.numInput `9`, FROM screens p LEFT JOIN screens p0 ON p0.screenId=p.screenId AND p0.userInput=0 LEFT JOIN screens p1 ON p1.screenId=p.screenId AND p1.userInput=1 LEFT JOIN screens p2 ON p2.screenId=p.screenId AND p2.userInput=2 LEFT JOIN screens p2 ON p3.screenId=p.screenId AND p3.userInput=3 LEFT JOIN screens p2 ON p4.screenId=p.screenId AND p4.userInput=4 LEFT JOIN screens p2 ON p5.screenId=p.screenId AND p5.userInput=5 LEFT JOIN screens p2 ON p6.screenId=p.screenId AND p6.userInput=6 LEFT JOIN screens p2 ON p7.screenId=p.screenId AND p7.userInput=7 LEFT JOIN screens p2 ON p8.screenId=p.screenId AND p8.userInput=8 LEFT JOIN screens p2 ON p9.screenId=p.screenId AND p9.userInput=9 GROUP BY p.screenId
答案 1 :(得分:1)
另一个非常糟糕的解决方案:)
SELECT DISTINCT screenId,
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '0' ) as '0',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '1' ) as '1',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '2' ) as '2',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '3' ) as '3',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '4' ) as '4',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '5' ) as '5',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '6' ) as '6',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '7' ) as '7',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '8' ) as '8',
(select IF(sum(numInput) IS NOT NULL, sum(numInput), 0) from quest as q where q.screenId = quest.screenId and q.userInput = '9' ) as '9'
from quest;
我想有一个更好的解决方案,你的“枢轴”就像使用组操作符的结果表一样。我发现时会更新。
答案 2 :(得分:1)
顺便提一下,标准解决方案(当表示层由于某些奇怪的原因而不可用时)如下。但请注意,表示OUTER JOIN实际上可能会快一些!
SELECT screenid
, MAX(CASE WHEN userinput = 0 THEN numinput ELSE 0 END) '0'
, MAX(CASE WHEN userinput = 1 THEN numinput ELSE 0 END) '1'
, MAX(CASE WHEN userinput = 2 THEN numinput ELSE 0 END) '2'
, MAX(CASE WHEN userinput = 3 THEN numinput ELSE 0 END) '3'
, MAX(CASE WHEN userinput = 4 THEN numinput ELSE 0 END) '4'
, MAX(CASE WHEN userinput = 5 THEN numinput ELSE 0 END) '5'
, MAX(CASE WHEN userinput = 6 THEN numinput ELSE 0 END) '6'
, MAX(CASE WHEN userinput = 7 THEN numinput ELSE 0 END) '7'
, MAX(CASE WHEN userinput = 8 THEN numinput ELSE 0 END) '8'
, MAX(CASE WHEN userinput = 9 THEN numinput ELSE 0 END) '9'
FROM my_table
GROUP
BY screenid;
哦,并且调用列'0','1','2','3'等等正在打开一个痛苦的世界