我有两张相同的表格,我保存了一个问题,姓氏和名字。其中一个是英国,另一个是美国。现在我想知道哪个人问的最多。我试了一下,找到了解决方案:
SELECT QUESTION , SURNAME , FORENAME , Count(*)
FROM table_UK
GROUP BY SURNAME,FORENAME
ORDER BY 1
DESC LIMIT 10
这也给了我想要的结果,但这仅适用于英国表。我想找出最受欢迎的十大人物。这意味着我必须将英国和美国的表格结合起来然后列出它们。我不想要英国的TOP 10和美国的前10名。他们应该混在一起
现在我发现了'UNION'命令组合表。所以我尝试了并执行了这个:
SELECT QUESTION , SURNAME , FORENAME , Count(*)
FROM table_UK
UNION
SELECT QUESTION , SURNAME , FORENAME , Count(*)
FROM table_US
GROUP BY SURNAME,FORENAME ORDER BY 1 DESC LIMIT 10
但是这给我带来了错误的结果。不知怎的,这是错误的。有人知道如何找到解决方案?
答案 0 :(得分:-1)
SELECT * FROM (
SELECT SURNAME , FORENAME , Count(*) as RANK
FROM table_UK
GROUP BY SURNAME , FORENAME
UNION
SELECT SURNAME , FORENAME , Count(*) as RANK
FROM table_US
GROUP BY SURNAME,FORENAME
) as t
ORDER BY t.RANK DESC
LIMIT 10