创建一个获取用户记录的排名,用户名和计数的视图

时间:2014-02-19 18:22:06

标签: php mysql sql

我有一张桌子。我无法创建显示w.r.t用户的记录数和来自另一个表和排名的用户名的视图,比如类别'1'。我如何创建视图?我真的很感激任何帮助。谢谢你。

CREATE TABLE if not exists tblA
(
id int(11) NOT NULL auto_increment ,
user varchar(255),
 category int(255),
 PRIMARY KEY (id)
);

CREATE TABLE if not exists tblB
(
id int(11) NOT NULL auto_increment ,
username varchar(255),
 userid int(255),
 PRIMARY KEY (id)
);


INSERT INTO tblA (user, category ) VALUES
('1', '1'),
('1', '2'),
('1', '3'),
('1', '1'),
('2', '1'),
('2', '1'),
('2', '1'),
('2', '1'),
('3', '1'),
('2', '1'),
('4', '1'),
('4', '1'),
('2', '1');


INSERT INTO tblB (userid, username ) VALUES
('1', 'A'),
('2', 'B'),
('3', 'C'),
('4', 'D'),
('5', 'E');

我试过但没有工作:

create view v as
SELECT
  tblB.username,
  groups.*,
  @rank:=@rank+1 AS rank
FROM
  (select 
    user,
    category,
    count(*) as num
  from 
    tblA
  where 
    category=1 
  group by 
    user, 
    category
  order by 
    num desc,
    user) AS groups
  -- left join: in case if data integrity fails:
  left join
    tblB ON groups.user=tblB.userid
  CROSS JOIN (SELECT @rank:=0) AS init

结果应该是:

  username   user      category     num   rank  
    B           2      1            6       1
    A           1      1            2       2
    D           4      1            2       3
    C           3      1            1       4

2 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。只需要进行微小的改动。以下查询将为您提供所需的结果。在内部查询获得前4列并获得排名交叉加入到(SELECT @curRank := 0) r这是获得排名的MySQL技巧。最后只需要通过Cnt订购才能使其正常工作。

SELECT username
    ,userid
    ,category
    ,Cnt
    ,@curRank := @curRank + 1 AS rank
    FROM (
            SELECT b.Username
                ,B.userid
                ,A.category
                ,count(*) Cnt
            FROM tblb B 
            JOIN tbla A
                ON B.UserID = A.User
            WHERE a.Category = 1
            GROUP BY b.username
        )a
,(SELECT @curRank := 0) r
Order by cnt desc

要将其置于View中,您可以使用@Gordon-Linoff in this question

描述的黑客攻击

结束代码看起来像这样。

CREATE VIEW TestView1
AS
    SELECT b.Username
           ,B.userid
           ,A.category
           ,COUNT(*) Cnt
        FROM tblb B
        JOIN tbla A
            ON B.UserID = A.User
        WHERE a.Category = 1
        GROUP BY b.username
        ORDER BY cnt DESC;

CREATE VIEW TestView2
AS
    SELECT t1.*
           ,( SELECT 1 + COUNT(*)
                FROM TestView1 AS t2
                WHERE t2.Cnt > t1.Cnt
                    OR (
                         t2.Cnt = t1.Cnt
                         AND t2.userid < t1.userid ) ) AS Rank
        FROM TestView1 AS t1

TestView1用于获取您定义的前4列。 TestView2您只需从第一个视图中选择所有内容,然后添加列,以检查您选择的值是否大于或大于该视图的第一个实例中的值。

答案 1 :(得分:0)

我确定SaUce的答案很好 - 但是那里的查询似乎仍然太多了!

SELECT username
     , user
     , category
     , COUNT(*) num
     , @i:=@i+1 rank 
  FROM tbla a 
  JOIN tblB b 
    ON b.userid = a.user
     , (SELECT @i:=0)var 
 WHERE category = 1 
 GROUP 
    BY user 
 ORDER 
    BY num DESC;