5

我有一个数据库,人们在其中为不同地方的人提交选票。在给定的时间,我想找出每个地方谁的票数最多。(一个人可以在两个不同的地方投票)

这是我到目前为止的 SQL:

SELECT placeId, userVotedId, cnt 
FROM 
    (SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
     FROM users as u, users_votes as uvo, places as p 
     WHERE u.userId = uvo.userVotedId 
       AND p.placeId = uvo.placeId 
     GROUP BY userVotedId, placeId) 
AS RESULT

这给了我这个结果:

在此处输入图像描述

现在,这些是我真正想要的行:

在此处输入图像描述

我的查询中缺少什么,所以我可以得到这个?

  • 我想要每个地方一个结果。所以我应该只看到不同的 placeId,其中 userVotedId 获得了最多的选票。

  • 在平局的情况下,随机获胜者将做!

4

3 回答 3

3

似乎您还需要一个聚合体。在您的价值上使用MAX()聚合和:cntGROUP BY placeId, userVotedId

SELECT placeId, userVotedId, max(cnt)
FROM 
(
  SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId
) AS RESULT
GROUP BY placeId, userVotedId

注意:我将您的查询更改为使用JOIN语法而不是表之间的逗号。

编辑,根据您的评论,以下应该有效:

select total.uservotedid,
  total.placeid,
  total.cnt
from
(
  SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId
) total
inner join
(
  select max(cnt) Mx, placeid
  from
  (
    SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
    FROM users as u
    INNER JOIN users_votes as uvo
      ON u.userId = uvo.userVotedId 
    INNER JOIN places as p 
      ON p.placeId = uvo.placeId 
    GROUP BY userVotedId, placeId
  ) mx
  group by placeid
) src
  on total.placeid = src.placeid
  and total.cnt = src.mx

请参阅带有演示的 SQL Fiddle

结果是:

| USERVOTEDID | PLACEID | CNT |
-------------------------------
|          65 |      11 |   1 |
|          67 |      13 |   1 |
|          67 |      25 |   1 |
|          67 |      51 |   2 |

编辑#2,如果你想在平局时返回一个随机数,那么你可以使用用户变量:

select uservotedid,
  placeid, 
  cnt
from
(
  select total.uservotedid,
    total.placeid,
    total.cnt,
    @rownum := case when @prev = total.placeid then @rownum+1 else 1 end rownum,
    @prev := total.placeid pplaceid
  from
  (
    SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
    FROM users as u
    INNER JOIN users_votes as uvo
      ON u.userId = uvo.userVotedId 
    INNER JOIN places as p 
      ON p.placeId = uvo.placeId 
    GROUP BY userVotedId, placeId
  ) total
  inner join
  (
    select max(cnt) Mx, placeid
    from
    (
      SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
      FROM users as u
      INNER JOIN users_votes as uvo
        ON u.userId = uvo.userVotedId 
      INNER JOIN places as p 
        ON p.placeId = uvo.placeId 
      GROUP BY userVotedId, placeId
    ) mx
    group by placeid
  ) src
    on total.placeid = src.placeid
    and total.cnt = src.mx
  order by total.placeid, total.uservotedid
) src
where rownum = 1
order by placeid, uservotedid

请参阅带有演示的 SQL Fiddle

于 2013-01-25T00:36:00.940 回答
1
SELECT placeId, userVotedId, MAX(cnt)
    FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) AS RESULT
    GROUP BY  placeId
于 2013-01-25T00:35:14.037 回答
1

为简单起见,我将您的查询称为测试:

SELECT * 
FROM Test T JOIN (
SELECT t.placeId, Max(t.cnt) maxcnt
FROM Test t
GROUP BY t.placeId) T2 ON T.placeId = T2.placeId and T.cnt = T2.maxcnt

这是小提琴

顺便说一句——测试=:

SELECT uvo.userVotedId, p.placeId, count(*) as cnt 
  FROM users as u
  INNER JOIN users_votes as uvo
    ON u.userId = uvo.userVotedId 
  INNER JOIN places as p 
    ON p.placeId = uvo.placeId 
  GROUP BY userVotedId, placeId

祝你好运。

--EDIT -- 根据要求,这是最终代码:

SELECT * 
FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) T JOIN (
SELECT t.placeId, Max(t.cnt) maxcnt
FROM (SELECT uvo.userVotedId, p.placeId, count(*) AS cnt 
          FROM users as u, users_votes as uvo, places as p 
          WHERE u.userId = uvo.userVotedId AND p.placeId = uvo.placeId 
          GROUP BY userVotedId, placeId) t
GROUP BY t.placeId) T2 ON T.placeId = T2.placeId and T.cnt = T2.maxcnt
于 2013-01-25T00:57:35.027 回答