在多列上进行枢轴风格计算
我有一个用户组的MySQL表,每个用户都有一个player1,player2和player3字段。
我正在计算所有玩家所选的玩家总数为3人。
即
user1可以selectplayerA,playerC和playerG。
user2可以selectplayerE,playerF和playerH。
user3可以selectplayerB,playerA和playerF。
user4可以selectplayerC,playerE和playerA。
我可以针对每个球员列进行声明,即
SELECT player1, count(*) as total from users; SELECT player2, count(*) as total from users; SELECT player3, count(*) as total from users;
问题是我需要一个包罗万象的结果集,它会显示所有选中的球员总数,所以我会在下面
player total playerA 3 playerB 1 playerC 2 playerE 2 playerF 2 playerG 1 playerH 1
我的表格结构如下
CREATE TABLE `leaderboard_api` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `competition_id` char(3) DEFAULT NULL, `year` year(4) DEFAULT NULL, `position` int(4) DEFAULT NULL, `name` varchar(128) DEFAULT NULL, `player1_name` varchar(128) DEFAULT NULL, `player1_score` tinyint(11) DEFAULT NULL, `player2_name` varchar(128) DEFAULT NULL, `player2_score` tinyint(11) DEFAULT NULL, `player3_name` varchar(128) DEFAULT NULL, `player3_score` tinyint(11) DEFAULT NULL, `total_score` tinyint(11) DEFAULT NULL, `missed_cut` tinyint(1) DEFAULT NULL, `withdrawn` tinyint(1) DEFAULT NULL, PRIMARY KEY (`id`) );
当我运行以下
select player1_name, count(player1_name) as total from leaderboard_api where year = '2015' and competition_id = '014' group by player1_name order by total desc;
我得到一个球员名单和他们各自的数量。 然后我可以运行这个分别为player2_name和player3_name并获得他们的总数。
我遇到的问题是每个玩家总计从每个查询到另一个得到一个总计。 我已经分别运行并获得预期的值。
我从@sagi下面的解决scheme尝试,但它不包括所有3个查询的总和,只给我1作为一个总数。
SELECT t.player,count(*) FROM ( SELECT player1 as player FROM `users` UNION ALL SELECT player2 FROM `users` UNION ALL SELECT player3 FROM `users` UNION ALL ....) t GROUP BY t.player
这是使用查询的前100名
select id, player1_name, player2_name, player3_name from leaderboard_api limit 100;
输出:
1 Donald, Luke Toms, David Bradley, Keegan 2 Stricker, Steve Bradley, Keegan Watson, Bubba 3 Choi, KJ Westwood, Lee Bradley, Keegan 4 Scott, Adam Stricker, Steve Immelman, Trevor 5 Stricker, Steve Garcia, Sergio Haas, Bill 6 Fowler, Rickie Schwartzel, Charl Bradley, Keegan 7 Watney, Nick Stricker, Steve Levin, Spencer 8 Stricker, Steve Johnson, Zach Bradley, Keegan 9 Donald, Luke Points, DA Fowler, Rickie 10 Westwood, Lee Donald, Luke Barnes, Ricky 11 Schwartzel, Charl Choi, KJ Haas, Bill 12 Woodland, Gary Choi, KJ Garcia, Sergio 13 Donald, Luke Stricker, Steve Molder, Bryce 14 Garcia, Sergio Kuchar, Matt Furyk, Jim 15 Noh, Seung-Yul Kuchar, Matt Watney, Nick 16 Donald, Luke Mickelson, Phil Barnes, Ricky 17 Scott, Adam Fowler, Rickie Noren, Alexander 18 Johnson, Zach Molinari, Francesco Toms, David 19 Choi, KJ Westwood, Lee Poulter, Ian 20 Mcilroy, Rory Points, DA Westwood, Lee 21 McIlroy, Rory Donald, Luke Na, Kevin 22 Choi, KJ Scott, Adam Dyson, Simon 23 Fowler, Rickie Johnson, Zach Bradley, Keegan 24 Mcilroy, Rory Scott, Adam Senden, John 25 Mickelson, Phil Kuchar, Matt Barnes, Ricky 26 Jimenez, Miguel Angel Mickelson, Phil Westwood, Lee 27 Donald, Luke Mcilroy, Rory Palmer, Ryan 28 Mickelson, Phil Choi, KJ Poulter, Ian 29 Mcilroy, Rory Mickelson, Phil Points, DA 30 Johnson, Zach Stricker, Steve Molinari, Francesco 31 Toms, David Love, Davis Mickelson, Phil 32 McIlroy, Rory Westwood, Lee Wilson, Mark 33 Mcilroy, Rory Garcia, Sergio Immelman, Trevor 34 Garcia, Sergio Love, Davis Westwood, Lee 35 Donald, Luke Stricker, Steve Sabbatini, Rory 36 Mcilroy, Rory Karlsson, Robert Romero, Andres 37 Choi, KJ Yang, YE Toms, David 38 Mcilroy, Rory Watney, Nick Kirk, Chris 39 Mcilroy, Rory Kuchar, Matt Noren, Alexander 40 Scott, Adam Yang, YE Romero, Andres 41 Donald, Luke Fowler, Rickie Jimenez, Miguel Angel 42 Donald, Luke Jimenez, Miguel Angel Fowler, Rickie 43 Donald, Luke Love, Davis Choi, KJ 44 Mcilroy, Rory Westwood, Lee Barnes, Ricky 45 Donald, Luke Fowler, Rickie Molinari, Edoardo 46 Garcia, Sergio Choi, KJ Yang, YE 47 Choi, KJ Fowler, Rickie Dyson, Simon 48 Fowler, Rickie Molinari, Francesco Johnson, Zach 49 Stricker, Steve Choi, KJ Casey, Paul 50 McIlroy, Rory Choi, KJ Molinari, Francesco 51 Fowler, Rickie Jimenez, Miguel Angel Mickelson, Phil 52 Mcilroy, Rory Garcia, Sergio Kim, Kyung-Tae 53 Mcilroy, Rory Garcia, Sergio Kim, Kyung-Tae 54 Mcilroy, Rory Mickelson, Phil Barnes, Ricky 55 Mcilroy, Rory Mickelson, Phil Barnes, Ricky 56 Fowler, Rickie Mcilroy, Rory Wilson, Mark 57 Fowler, Rickie Love, Davis Watson, Bubba 58 Fowler, Rickie Harrington, Padraig Poulter, Ian 59 McIlroy, Rory Mickelson, Phil Jimenez, Miguel Angel 60 Mcilroy, Rory Fowler, Rickie Jimenez, Miguel Angel 61 Day, Jason Johnson, Zach Bradley, Keegan 62 Stricker, Steve Overton, Jeff Mickelson, Phil 63 Day, Jason Furyk, Jim Toms, David 64 Donald, Luke Westwood, Lee Gay, Brian 65 Stricker, Steve Day, Jason Kim, Kyung-Tae 66 Mahan, Hunter Stricker, Steve Van Pelt, Bo 67 Day, Jason Scott, Adam Barnes, Ricky 68 Day, Jason Johnson, Zach Love, Davis 69 Fowler, Rickie Jacobson, Fredrik Bradley, Keegan 70 Karlsson, Robert Watney, Nick Quiros, Alvaro 71 Schwartzel, Charl Johnson, Dustin Love, Davis 72 Mcilroy, Rory Scott, Adam Slocum, Heath 73 Day, Jason Johnson, Zach Dyson, Simon 74 Day, Jason Johnson, Zach Dyson, Simon 75 Day, Jason Mcilroy, Rory Garrigus, Robert 76 Donald, Luke Mcilroy, Rory Byrd, Jonathan 77 Baddeley, Aaron Garcia, Sergio Toms, David 78 Day, Jason Kuchar, Matt Sabbatini, Rory 79 Johnson, Dustin Choi, KJ Fisher, Ross 80 Simpson, Webb Stricker, Steve Yang, YE 81 Day, Jason Mickelson, Phil Barnes, Ricky 82 Johnson, Zach Howell, Charles Snedeker, Brandt 83 Donald, Luke Mcilroy, Rory Stallings, Scott 84 Kim, Anthony Mickelson, Phil Rose, Justin 85 Kaymer, Martin Marino, Steve Westwood, Lee 86 Mahan, Hunter Kaymer, Martin Poulter, Ian 87 Mcilroy, Rory Johnson, Dustin Jimenez, Miguel Angel 88 Mcilroy, Rory Johnson, Dustin Jimenez, Miguel Angel 89 Laird, Martin Scott, Adam Snedeker, Brandt 90 Johnson, Dustin Johnson, Zach Manassero, Matteo 91 Day, Jason Watney, Nick Tringale, Cameron 92 Garcia, Sergio Day, Jason Quiros, Alvaro 93 Day, Jason Moore, Ryan Toms, David 94 Day, Jason Mcilroy, Rory Barnes, Ricky 95 Baddeley, Aaron Mcilroy, Rory Yang, YE 96 Westwood, Lee Jacobson, Fredrik Kim, Kyung-Tae 97 Day, Jason Mcilroy, Rory Molinari, Edoardo 98 Day, Jason Garcia, Sergio Cink, Stewart 99 Bjorn, Thomas Scott, Adam Woods, Tiger 100 Day, Jason Johnson, Dustin Levin, Spencer
如果我正确地理解了你,你可以使用UNION ALL
:
SELECT t.player,count(*) FROM ( SELECT player1 as player FROM `users` UNION ALL SELECT player2 FROM `users` UNION ALL SELECT player3 FROM `users` UNION ALL ....) t GROUP BY t.player
编辑:我认为你的名字中有空格,所以他们不是分组为一个组,试试这个:
SELECT replace(t.player,' ',''),count(*) FROM ( SELECT player1 as player FROM `users` UNION ALL SELECT player2 FROM `users` UNION ALL SELECT player3 FROM `users` UNION ALL ....) t GROUP BY replace(t.player,' ','')
您也可以尝试replace这一行:
replace(t.player,' ','')
有了这一行:
trim(t.player)
取决于你的数据的样子。
在从上面的@sagi开始解决问题之后,我终于find了答案,如果这是正确的语言,子表中的计数就会丢失。
这是我的解决scheme,可能需要它的人…
select t.player, sum(count) as total from ( select player1_name as player, count(player1_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player1_name UNION select player2_name, count(player2_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player2_name UNION select player3_name, count(player3_name) as count from leaderboard_api where year = '2015' and competition_id = '033' group by player3_name) t group by t.player order by total desc