我正在尝试为我的表获取行号。到目前为止,这是我的 SQL 提示:
SELECT @i = @i + 1 AS Rank, x.Name, x.PartyName, x.ConstituencyName, x.Votes
FROM (
SELECT CONCAT(t1.Firstname, ' ', t1.Lastname) AS Name, t1.PartyName, t1.ConstituencyName, COALESCE(t2.Count, 0) AS Votes
FROM (
(SELECT db.user.PID, db.user.Firstname, db.user.Lastname, db.party.PartyName, db.constituency.ConstituencyName
FROM db.user
LEFT JOIN db.party ON db.user.PartyId = db.party.PartyID
LEFT JOIN db.constituency ON db.user.CID = db.constituency.CID
WHERE db.user.PartyId IS NOT NULL
AND db.user.CID IS NOT NULL
) t1
LEFT JOIN
(SELECT db.user.Vote, COUNT(*) AS 'Count'
FROM db.user
GROUP BY db.user.Vote
) t2
ON
t1.PID = t2.Vote
ORDER BY t2.Count DESC)
) x,
(SELECT @i:=1) r
所以这基本上做的是,首先我创建一个表 t1(从 2 个不同的表中获取信息),然后将它与表 t2 连接起来,这样我就可以得到每个候选人的总票数。然后我将该表标记为“x”并将其与应该初始化行号的表“r”连接(使用行号的在线教程)。但是,当我运行此 SQL 提示时,我得到以下回报:
Rank Name PartyName ConstituencyName Votes
0 Name1 Party1 Constituency1 700
0 Name2 Party1 Constituency1 550
任何建议将不胜感激。