我每次运行时都试图获得查询的排名,但似乎无法正确查看。 此外,我希望能够显示相同的排名行与相同的数字。即1,2,2,4,5,5,5,8。
我对查询进行了一次尝试,但我只能根据表中记录的原始顺序获得排名,而不是根据我的查询执行的排序。
任何帮助将不胜感激。干杯
SET @rank:=0;
SELECT @rank:=@rank+1 AS rank
, masters_2014.name
, masters_2014.player1
, player1_score.hole as player1_thru
, player1_score.to_par_s as player1_to_par
, masters_2014.player2
, player2_score.hole as player2_thru
, player2_score.to_par_s as player2_to_par
, masters_2014.player3
, player3_score.hole as player3_thru
, player3_score.to_par_s as player3_to_par
, (player1_score.`to_par` + player2_score.`to_par` + player3_score.`to_par`) as team_total
FROM masters_2014
INNER
JOIN leaderboard_A as player1_score
ON player1_score.name = masters_2014.player1
AND player1_score.tournament_name = 'masters tournament'
AND player1_score.year = 2014
INNER
JOIN leaderboard_A as player2_score
ON player2_score.name = masters_2014.player2
AND player2_score.tournament_name = 'masters tournament'
AND player2_score.year = 2014
INNER
JOIN leaderboard_A as player3_score
ON player3_score.name = masters_2014.player3
AND player3_score.tournament_name = 'masters tournament'
AND player3_score.year = 2014
ORDER BY team_total
CREATE TABLE IF NOT EXISTS `leaderboard_A` (
`league` tinyint(1) NOT NULL,
`id` bigint(10) NOT NULL,
`cut` tinyint(1) NOT NULL,
`wd` tinyint(1) NOT NULL,
`tie` tinyint(1) NOT NULL,
`pos` int(4) NOT NULL,
`pos_s` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`to_par` smallint(3) NOT NULL,
`to_par_s` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`hole` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`round` smallint(6) NOT NULL,
`round_s` varchar(8) COLLATE utf8_unicode_ci NOT NULL,
`round_1` int(11) NOT NULL,
`round_2` int(11) NOT NULL,
`round_3` int(11) NOT NULL,
`round_4` int(11) NOT NULL,
`total` smallint(4) NOT NULL,
`tournament_id` varchar(13) COLLATE utf8_unicode_ci NOT NULL,
`tournament_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`year` smallint(4) NOT NULL,
PRIMARY KEY (`league`,`id`,`tournament_id`),
KEY `pos` (`pos`),
KEY `player_key` (`name`,`tournament_name`,`year`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `leaderboard_A`
--
INSERT INTO `leaderboard_A` (`name`, `to_par`, `to_par_s`, `tournament_name`, `year`) VALUES
('Couples, Fred', 2, '2', 'Masters Tournament', 2014),
('Langer, Bernhard', 0, 'Par', 'Masters Tournament', 2014),
('Lyle, Sandy', 9, '9', 'Masters Tournament', 2014),
('Mize, Larry', 16, '16', 'Masters Tournament', 2014),
('Olazabal, Jose Maria', 6, '6', 'Masters Tournament', 2014),
('Stricker, Steve', 5, '5', 'Masters Tournament', 2014),
('Singh, Vijay', 7, '7', 'Masters Tournament', 2014),
('Weir, Mike', 9, '9', 'Masters Tournament', 2014),
('Furyk, Jim', 1, '1', 'Masters Tournament', 2014),
('Bjorn, Thomas', 0, 'Par', 'Masters Tournament', 2014),
('Clarke, Darren', 9, '9', 'Masters Tournament', 2014),
('Jimenez, Miguel Angel', -4, '-4', 'Masters Tournament', 2014),
('Cink, Stewart', 1, '1', 'Masters Tournament', 2014),
('Gallacher, Stephen', 6, '6', 'Masters Tournament', 2014),
('Westwood, Lee', -1, '-1', 'Masters Tournament', 2014),
('Senden, John', 0, 'Par', 'Masters Tournament', 2014),
('Stenson, Henrik', 1, '1', 'Masters Tournament', 2014),
('Rose, Justin', 1, '1', 'Masters Tournament', 2014),
('Kuchar, Matt', -2, '-2', 'Masters Tournament', 2014),
('de Jonge, Brendon', 7, '7', 'Masters Tournament', 2014),
('Poulter, Ian', 2, '2', 'Masters Tournament', 2014),
('Choi, K.J.', 6, '6', 'Masters Tournament', 2014),
('Scott, Adam', 1, '1', 'Masters Tournament', 2014),
('Jaidee, Thongchai', 7, '7', 'Masters Tournament', 2014),
('Mahan, Hunter', 4, '4', 'Masters Tournament', 2014),
('Haas, Bill', 2, '2', 'Masters Tournament', 2014),
('Fernandez-Castano, Gonzalo', 4, '4', 'Masters Tournament', 2014),
('Molinari, Francesco', 11, '11', 'Masters Tournament', 2014),
('Bowditch, Steven', 4, '4', 'Masters Tournament', 2014),
('Donaldson, Jamie', 1, '1', 'Masters Tournament', 2014),
('Walker, Jimmy', 0, 'Par', 'Masters Tournament', 2014),
('Watson, Bubba', -8, '-8', 'Masters Tournament', 2014),
('Glover, Lucas', 8, '8', 'Masters Tournament', 2014),
('Oosthuizen, Louis', 3, '3', 'Masters Tournament', 2014),
('Stadler, Kevin', 0, 'Par', 'Masters Tournament', 2014),
('Watney, Nick', 9, '9', 'Masters Tournament', 2014),
('Streelman, Kevin', 8, '8', 'Masters Tournament', 2014),
('Kaymer, Martin', 5, '5', 'Masters Tournament', 2014),
('Snedeker, Brandt', 7, '7', 'Masters Tournament', 2014),
('Blixt, Jonas', -5, '-5', 'Masters Tournament', 2014),
('Day, Jason', 2, '2', 'Masters Tournament', 2014),
('McIlroy, Rory', 0, 'Par', 'Masters Tournament', 2014),
('Luiten, Joost', 4, '4', 'Masters Tournament', 2014),
('Horschel, Billy', 7, '7', 'Masters Tournament', 2014),
('Kirk, Chris', 1, '1', 'Masters Tournament', 2014),
('Woodland, Gary', 4, '4', 'Masters Tournament', 2014),
('Fowler, Rickie', -2, '-2', 'Masters Tournament', 2014),
('Olesen, Thorbjorn', 9, '9', 'Masters Tournament', 2014),
('Spieth, Jordan', -5, '-5', 'Masters Tournament', 2014),
('Henley, Russell', 5, '5', 'Masters Tournament', 2014),
('Goss, Oliver', 10, '10', 'Masters Tournament', 2014);
CREATE TABLE `masters_2014` (
`name` varchar(128) NOT NULL,
`player1` varchar(128) NOT NULL,
`player2` varchar(128) NOT NULL,
`player3` varchar(128) NOT NULL,
UNIQUE KEY `name` (`name`),
KEY `player1_key` (`player1`),
KEY `player2_key` (`player2`),
KEY `player3_key` (`player3`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `masters_2014`
--
INSERT INTO `masters_2014` (`name`, `player1`, `player2`, `player3`) VALUES
('Team 1', 'Fowler, Rickie', 'Henley, Russell', 'McIlroy, Rory'),
('Team 10', 'Gallacher, Stephen', 'Scott, Adam', 'Spieth, Jordan'),
('Team 11', 'Luiten, Joost', 'McIlroy, Rory', 'Scott, Adam'),
('Team 12', 'Day, Jason', 'Jimenez, Miguel Angel', 'McIlroy, Rory'),
('Team 13', 'Day, Jason', 'Jimenez, Miguel Angel', 'Scott, Adam'),
('Team 14', 'Furyk, Jim', 'Kaymer, Martin', 'Scott, Adam'),
('Team 15', 'Day, Jason', 'Kuchar, Matt', 'Streelman, Kevin'),
('Team 16', 'Choi, K.J.', 'Fowler, Rickie', 'Poulter, Ian'),
('Team 17', 'Jimenez, Miguel Angel', 'Kuchar, Matt', 'McIlroy, Rory'),
('Team 18', 'Day, Jason', 'Luiten, Joost', 'Scott, Adam'),
('Team 19', 'Kuchar, Matt', 'Molinari, Francesco', 'Stricker, Steve'),
('Team 2', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Stenson, Henrik'),
('Team 20', 'Furyk, Jim', 'Horschel, Billy', 'Kuchar, Matt'),
('Team 21', 'Jimenez, Miguel Angel', 'Kuchar, Matt', 'McIlroy, Rory'),
('Team 22', 'Kuchar, Matt', 'Senden, John', 'Westwood, Lee'),
('Team 23', 'Couples, Fred', 'Fowler, Rickie', 'McIlroy, Rory'),
('Team 24', 'Day, Jason', 'Jimenez, Miguel Angel', 'Scott, Adam'),
('Team 25', 'Kaymer, Martin', 'Rose, Justin', 'Westwood, Lee'),
('Team 26', 'Jimenez, Miguel Angel', 'Rose, Justin', 'Scott, Adam'),
('Team 27', 'Westwood, Lee', 'Watson, Bubba', 'Couples, Fred'),
('Team 28', 'Day, Jason', 'Jimenez, Miguel Angel', 'Kuchar, Matt'),
('Team 29', 'Choi, K.J.', 'Oosthuizen, Louis', 'Poulter, Ian'),
('Team 3', 'Choi, K.J.', 'Day, Jason', 'Donaldson, Jamie'),
('Team 30', 'Day, Jason', 'Gallacher, Stephen', 'McIlroy, Rory'),
('Team 31', 'Day, Jason', 'Jimenez, Miguel Angel', 'McIlroy, Rory'),
('Team 32', 'Couples, Fred', 'McIlroy, Rory', 'Woodland, Gary'),
('Team 33', 'Day, Jason', 'Fernandez-Castano, Gonzalo', 'Westwood, Lee'),
('Team 34', 'Day, Jason', 'Senden, John', 'Spieth, Jordan'),
('Team 35', 'Fowler, Rickie', 'Horschel, Billy', 'Westwood, Lee'),
('Team 36', 'Jimenez, Miguel Angel', 'Snedeker, Brandt', 'Stenson, Henrik'),
('Team 37', 'McIlroy, Rory', 'Singh, Vijay', 'Watson, Bubba'),
('Team 38', 'Fernandez-Castano, Gonzalo', 'McIlroy, Rory', 'Westwood, Lee'),
('Team 39', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Scott, Adam'),
('Team 4', 'Fowler, Rickie', 'Haas, Bill', 'Woodland, Gary'),
('Team 40', 'Day, Jason', 'Jimenez, Miguel Angel', 'McIlroy, Rory'),
('Team 41', 'Bjorn, Thomas', 'Snedeker, Brandt', 'Watson, Bubba'),
('Team 42', 'Jimenez, Miguel Angel', 'Scott, Adam', 'Watson, Bubba'),
('Team 43', 'Donaldson, Jamie', 'Fowler, Rickie', 'Spieth, Jordan'),
('Team 44', 'Couples, Fred', 'Fowler, Rickie', 'Scott, Adam'),
('Team 45', 'Olesen, Thorbjorn', 'Oosthuizen, Louis', 'Stricker, Steve'),
('Team 46', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Scott, Adam'),
('Team 47', 'Couples, Fred', 'Donaldson, Jamie', 'McIlroy, Rory'),
('Team 48', 'Bjorn, Thomas', 'Day, Jason', 'Walker, Jimmy'),
('Team 49', 'Day, Jason', 'Scott, Adam', 'Stadler, Kevin'),
('Team 5', 'Couples, Fred', 'Fowler, Rickie', 'Kuchar, Matt'),
('Team 50', 'Day, Jason', 'Jimenez, Miguel Angel', 'Scott, Adam'),
('Team 51', 'Jimenez, Miguel Angel', 'Kuchar, Matt', 'Scott, Adam'),
('Team 52', 'Day, Jason', 'Scott, Adam', 'Stadler, Kevin'),
('Team 53', 'Day, Jason', 'Haas, Bill', 'Molinari, Francesco'),
('Team 54', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Walker, Jimmy'),
('Team 55', 'Kaymer, Martin', 'McIlroy, Rory', 'Stricker, Steve'),
('Team 56', 'Horschel, Billy', 'Rose, Justin', 'Westwood, Lee'),
('Team 57', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Scott, Adam'),
('Team 58', 'Kaymer, Martin', 'Oosthuizen, Louis', 'Rose, Justin'),
('Team 59', 'De Jonge, Brendon', 'Kuchar, Matt', 'Scott, Adam'),
('Team 6', 'Bjorn, Thomas', 'Scott, Adam', 'Westwood, Lee'),
('Team 60', 'Cink, Stewart', 'Snedeker, Brandt', 'Watson, Bubba'),
('Team 61', 'Fowler, Rickie', 'Furyk, Jim', 'Watney, Nick'),
('Team 62', 'Henley, Russell', 'Scott, Adam', 'Spieth, Jordan'),
('Team 63', 'Day, Jason', 'Luiten, Joost', 'McIlroy, Rory'),
('Team 64', 'Kaymer, Martin', 'Poulter, Ian', 'Rose, Justin'),
('Team 65', 'Couples, Fred', 'Furyk, Jim', 'Scott, Adam'),
('Team 66', 'Donaldson, Jamie', 'Fowler, Rickie', 'Spieth, Jordan'),
('Team 67', 'Cink, Stewart', 'McIlroy, Rory', 'Walker, Jimmy'),
('Team 68', 'Kuchar, Matt', 'Langer, Bernhard', 'Scott, Adam'),
('Team 69', 'Jimenez, Miguel Angel', 'Kuchar, Matt', 'McIlroy, Rory'),
('Team 7', 'Day, Jason', 'Kuchar, Matt', 'Stadler, Kevin'),
('Team 70', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Scott, Adam'),
('Team 71', 'Day, Jason', 'Jimenez, Miguel Angel', 'Scott, Adam'),
('Team 72', 'Furyk, Jim', 'Kaymer, Martin', 'Stenson, Henrik'),
('Team 73', 'Bjorn, Thomas', 'Scott, Adam', 'Spieth, Jordan'),
('Team 74', 'Gallacher, Stephen', 'Rose, Justin', 'Scott, Adam'),
('Team 75', 'Choi, K.J.', 'Snedeker, Brandt', 'Stricker, Steve'),
('Team 76', 'Day, Jason', 'Jimenez, Miguel Angel', 'McIlroy, Rory'),
('Team 77', 'McIlroy, Rory', 'Scott, Adam', 'Stadler, Kevin'),
('Team 78', 'Fowler, Rickie', 'Kaymer, Martin', 'McIlroy, Rory'),
('Team 79', 'Fernandez-Castano, Gonzalo', 'Rose, Justin', 'Watson, Bubba'),
('Team 8', 'Kuchar, Matt', 'Senden, John', 'Walker, Jimmy'),
('Team 9', 'Jimenez, Miguel Angel', 'McIlroy, Rory', 'Scott, Adam');
答案 0 :(得分:0)
对于你的问题How difficult is it also to have "T" before any ties?
......这并不困难,但我不能在我的结果上做到这一点,因为我没有关系的任何数据..
(1。如果你想编辑表格来添加“T”)
UPDATE leaderboard_A SET tie = Concat('T', tie);
(2。如果你想在不改变数据库的情况下将“T”添加到关系中)
SELECT
CONCAT('T', tie) AS ties,
masters_2014.name
, masters_2014.player1
, player1_score.to_par_s AS player1_to_par
, masters_2014.player2
, player2_score.to_par_s AS player2_to_par
, masters_2014.player3
, player3_score.to_par_s AS player3_to_par
, (player1_score.`to_par` + player2_score.`to_par` + player3_score.`to_par`) AS team_total
FROM masters_2014
(3。如果你的意思是在等级中添加“T”),请参阅THIS_FIDDLE
你需要做的就是IF
这样的concat
IF (counter = 1, @rank:= placeholder, CONCAT("T ", @rank)) AS ranker
为了你的回答 查看新的FIDDLE以获得最终结果---
我必须要做的是获得一个每行计数+1的占位符..然后在设置@rank等于它时每次你有不同的分数时将其放入..或者只是放入@rank因为它等于占位符对该分数的第一个值。我最初获得了一些不稳定的结果,因为占位符@c被设置为每行返回的行数(55)+1 ...而不是从0开始它是从55开始......我想这是只是sqlfiddle的一个错误,因为它不应该这样做...所以让我知道它是否在你的环境中这样做。这是SQL:
SET @rank :=0, @a :=0, @b :=0, @c :=0;
SELECT
name,
player1,
player1_to_par,
player2,
player2_to_par,
player3,
player3_to_par,
team_total,
IF (counter = 1, @rank:= placeholder, @rank) AS ranker
FROM(
SELECT
*,
@c := @c +1 AS placeholder,
IF(@a <> team_total, @b := @b := 1, @b :=0) AS counter,
@a := team_total
from(
SELECT
masters_2014.name
, masters_2014.player1
, player1_score.to_par_s AS player1_to_par
, masters_2014.player2
, player2_score.to_par_s AS player2_to_par
, masters_2014.player3
, player3_score.to_par_s AS player3_to_par
, (player1_score.`to_par` + player2_score.`to_par` + player3_score.`to_par`) AS team_total
FROM masters_2014
INNER JOIN leaderboard_A AS player1_score
ON player1_score.name = masters_2014.player1
AND player1_score.tournament_name = 'masters tournament'
AND player1_score.year = 2014
INNER JOIN leaderboard_A AS player2_score
ON player2_score.name = masters_2014.player2
AND player2_score.tournament_name = 'masters tournament'
AND player2_score.year = 2014
INNER JOIN leaderboard_A AS player3_score
ON player3_score.name = masters_2014.player3
AND player3_score.tournament_name = 'masters tournament'
AND player3_score.year = 2014
ORDER BY team_total
)AS t
)AS FINAL