在一行中组合2个MySQL SELECT

时间:2014-05-10 21:15:55

标签: mysql select

如何在一行上写下这个2 SELECT。我需要获得2个查询。第一个问题是:

SELECT (COUNT(*) + 1) AS rank FROM `rang_info` WHERE `player_kills` > (SELECT `player_kills` FROM `rang_info` WHERE `player_name` = '%s')

第二个问题是:

SELECT COUNT(*) FROM `rang_info`

2 个答案:

答案 0 :(得分:1)

使用UNION。应该工作:

SELECT (COUNT(*) + 1) AS rank FROM `rang_info` WHERE `player_kills` > (SELECT `player_kills` FROM `rang_info` WHERE `player_name` = '%s')
UNION
SELECT COUNT(*) FROM `rang_info`

答案 1 :(得分:0)

如果你想要一行,那么下面的内容应该可以正常工作

SELECT SUM(rank) AS rank, SUM(total) AS total 
FROM
(SELECT (COUNT(*) + 1) AS rank, 0 AS total FROM `rang_info` 
WHERE `player_kills` > (SELECT `player_kills` FROM `rang_info` WHERE `player_name` = '%s')
UNION
SELECT 0 AS rank, COUNT(*) AS total FROM `rang_info`) AS temptable