2014-02-11 20 views
5

我想獲得最高點行與行列和選擇查詢沒有rownum = rownum + 1。我已經嘗試了下面的查詢,但我缺少的東西也鏈接http://sqlfiddle.com/#!2/fd7897/7排名用戶由最大id和點,但(排名是錯誤的)

我在尋找每個接收者的最後一個入口的答案,這也是最高分入口rankwise:我真的很感激任何幫助。感謝提前。

像這樣:

( '2', '4', '測試...', '2011-08-21 14時13分19秒', '40', '009') - --rank1

('4','2','test ...','2011-08-21 14:13:19','30','003')---- rank2

( '1', '3', '測試...', '2011-08-21 14點12分19秒', '20', '005')---- rank3

查詢到目前爲止:

SELECT * , 
      (select count(*) 
      from tblA u2 
      where u2.points > u.points or 
        u2.points = u.points and u2.id <= u.id 
      ) as rank 
FROM (SELECT u.receiver, MAX(u.id) AS id 
     FROM tblA u 
     GROUP BY u.receiver 
    ) subset JOIN 
    tblA u 
    ON subset.receiver = u.receiver AND subset.id = u.id order by rank; 

表:

CREATE TABLE if not exists tblA 
(
id int(11) NOT NULL auto_increment , 
sender varchar(255), 
receiver varchar(255), 
msg varchar(255), 
date timestamp, 
    points int(255), 
    refno varchar(255), 
PRIMARY KEY (id) 
); 

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

CREATE TABLE if not exists tblC 
(
id int(11) NOT NULL auto_increment , 
data varchar(255), 
    refno varchar(255), 
    extrarefno varchar(255), 
PRIMARY KEY (id) 
); 


INSERT INTO tblA (sender, receiver,msg,date,points,refno) VALUES 
('1', '2', 'buzz ...','2011-08-21 14:11:09','10','001'), 
('1', '2', 'test ...','2011-08-21 14:12:19','20','002'), 
('4', '2', 'test ...','2011-08-21 14:13:19','30','003'), 
('1', '3', 'buzz ...','2011-08-21 14:11:09','10','004'), 
('1', '3', 'test ...','2011-08-21 14:12:19','20','005'), 
('1', '4', 'buzz ...','2011-08-21 14:11:09','10','006'), 
('1', '4', 'test ...','2011-08-21 14:12:19','20','007'), 
('3', '4', 'test ...','2011-08-21 14:13:19','30','008'), 
('2', '4', 'test ...','2011-08-21 14:13:19','40','009'); 



INSERT INTO tblB (sno, name) VALUES 
('1', 'Aa'), 
('2', 'Bb'), 
('3', 'Cc'), 
('4', 'Dd'), 
('5', 'Ee'), 
('6', 'Ff'), 
('7', 'Gg'), 
('8', 'Hh'); 


INSERT INTO tblC (data,refno,extrarefno) VALUES 
('data1', '001', '101'), 
('data2', '002', '102'), 
('data3', '003', '103'), 
('data4', '004', '101'), 
('data5', '005', '102'), 
('data6', '006', '103'), 
('data7', '007', '101'), 
('data8', '008', '101'), 
('data9', '009', '101'); 
+1

+1的數據例如,小提琴,所有需要的細節:) –

+0

@JorgeCampos謝謝,但掙扎了精確的輸出,子查詢工作的一部分,而不是整個查詢。 – jason

+0

我有點困惑。你爲什麼需要tblB和tblC? – Leo

回答

2

問題是子查詢中的count(*)。將其更改爲count(distinct receiver)

SELECT * , 
     (select count(distinct receiver) 
     from tblA u2 
     where u2.points > u.points or 
       u2.points = u.points and u2.id <= u.id 
     ) as rank 
FROM (SELECT u.receiver, MAX(u.id) AS id 
     FROM tblA u 
     GROUP BY u.receiver 
    ) subset JOIN 
    tblA u 
    ON subset.receiver = u.receiver AND subset.id = u.id 
order by rank; 

編輯:

要創建這個作爲MySQL的觀點,你必須得到from子句中的聚集權:

SELECT * , 
     (select count(distinct receiver) 
     from tblA u2 
     where u2.points > u.points or 
       u2.points = u.points and u2.id <= u.id 
     ) as rank 
FROM tblA u 
WHERE u.id = (select max(u2.id) from tblA u2 where u2.receiver = u.receiver) 
order by rank; 
+0

先生,我試圖在上面的語句之前添加'create view unique_users_ranks as',並且我得到'1349 - View的SELECT包含FROM子句中的子查詢'我嘗試了所有東西,但仍然被卡在視圖部分。如何將其轉換上面的查詢到一個視圖。這是我的最終目標。請幫助。 – jason

+0

我正在實施你所建議的所有觀點和疑問,以及它的工作主席先生。你是最好的。再次感謝主席先生,當我卡住時,我會繼續向你發問。再次感謝主席先生的所有幫助。我真的很感激他。 – jason

1

怎麼樣?

SELECT a.* 
FROM tbla a, 
     (SELECT receiver, 
       Max(points) AS m 
     FROM tbla 
     GROUP BY receiver) AS b 
WHERE a.receiver = b.receiver 
     AND a.points = b.m 
ORDER BY m DESC  
1

請注意,相關MySQL的子查詢性能較差。我認爲下面的查詢返回結果與您的結果相同並且速度很快。

SELECT x.*, @ord := @ord + 1 AS rank 
FROM (
    SELECT u.* 
    FROM(SELECT u.receiver, MAX(u.id) AS id 
      FROM tblA u 
      GROUP BY u.receiver 
    ) subset INNER JOIN tblA u ON subset.receiver = u.receiver AND subset.id = u.id, 
    (SELECT @ord := 0) init 
    ORDER BY points DESC 
) x 
ORDER BY rank; 
+----+--------+----------+----------+---------------------+--------+-------+------+ 
| id | sender | receiver | msg  | date    | points | refno | rank | 
+----+--------+----------+----------+---------------------+--------+-------+------+ 
| 9 | 2  | 4  | test ... | 2011-08-21 14:13:19 |  40 | 009 | 1 | 
| 3 | 4  | 2  | test ... | 2011-08-21 14:13:19 |  30 | 003 | 2 | 
| 5 | 1  | 3  | test ... | 2011-08-21 14:12:19 |  20 | 005 | 3 | 
+----+--------+----------+----------+---------------------+--------+-------+------+ 
+0

但我無法在VIEW.Can中創建上述查詢,可以將上述內容轉換爲視圖 – jason

+0

@jason抱歉。我不知道你在VIEW中詢問過一個查詢。根據MySQL Manaul(https://dev.mysql.com/doc/refman/5.5/en/create-view.html),我們不能在VIEW中使用用戶變量。 –