2012-08-29 27 views
0
id one two thr fou five 
1 37 84 1 68 10 
2 72 50 87 41 67 
3 66 30 89 57 48 
4 29 27 35 75 36 
5 2 72 9 1 55 
6 33 89 17 40 64 
7 70 90 63 26 54 
8 36 19 51 43 61 
9 10 61 20 44 84 
10 2 41 43 65 87 

我需要反轉計數上表中的每個數字。在mySQL中的反向計數

例子:

61=>1 because if you count from the bottom to the first 61 there's 1 row 
26=>3 because if you count from the bottom to the first 26 there are 3 rows 
9=>5 because if you count from the bottom to the first 9 there are 5 rows 
and so on... 

查詢應輸出類似於所有數字如下表:

Number Rows count 
61 1 
26 3 
9 5 

這裏的問題是:如何扭轉計數的MySQL?有特殊功能嗎? 謝謝

+0

如何表有序?通過'ID'?如果是這樣,是否確保'id'是連續的(即沒有間隙)? – eggyal

+1

你有什麼嘗試嗎? –

+0

@eggyal ID不是連續的 - juergend我沒有嘗試任何東西,因爲我不知道如何從底部計算行... – Floppy88

回答

0
select COUNT(*) from TestTbl 
where id > (select max(id) from TestTbl 
where one='9' or two ='9' or three='9' or four='9' or five='9') 

select COUNT(*) from TestTbl 
where id > (select max(id) from TestTbl 
where one='26' or two ='26' or three='26' or four='26' or five='26') 

select COUNT(*) from TestTbl 
where id > (select max(id) from TestTbl 
where one='61' or two ='61' or three='61' or four='61' or five='61') 
+0

謝謝,但是查詢應該自動執行此操作所有數字包含在我的帖子的第一個表格中。我不應該在查詢中寫入每個數字。 – Floppy88

+0

這將是很難做到這一點,因爲你試圖跨越太多的障礙(1)你必須旋轉表獲取每個數字(2),那麼你必須關聯該數字以獲得最大通過一個子查詢。所以,更好的選擇是編寫一個存儲過程來完成它。 –

+0

我的解決方案對我來說工作得很好。我測試了它,它做了什麼問。沒有那麼難完成。 – jasonmclose

0

這裏是我想出了。它很醜。

select num, t1.max - t2.id as reverse_count from 
(select max(id) as max from testtable) as t1, 
(select id, row1 as num from testtable union all select id, 
row2 as num from testtable union all select id, 
row3 as num from testtable union all select id, 
row4 as num from testtable) as t2; 

編輯使用正確的命名方案

select num as Number, t1.max - t2.id as RowsCount from 
(select max(id) as max from testtable) as t1, 
(select id, one as num from testtable union all select id, 
two as num from testtable union all select id, 
thr as num from testtable union all select id, 
fou as num from testtable union all select id, 
five as num from testtable) as t2; 
+0

我沒有按照你的命名模式。抱歉。 row1應該被認爲是你的專欄'one',row2應該是'two'。匆忙。 – jasonmclose

+0

我試過你的解決方案,但它需要太多的時間來加載(我的PC與XAMPP大約5分鐘!) – Floppy88

+0

真的嗎?哇。如果您有能力將其分解爲2個查詢並獲取MAX(id),以便可以將它傳遞給變量,則可以使用查詢的後半部分。所以,'從測試表中選擇MAX(id),然後將它插入到函數的後半部分(假設你有變量$ MAX)'select t2.num as Number,($ MAX - t2.id)as RowsCount從testtable union中選擇id,一個作爲num所有選擇id,兩個作爲testtable union中的num all從testtable union中選擇id,thr作爲num所有選擇id,fou作爲num從testtable union中選擇id,五作爲來自testtable的num )作爲t2;' – jasonmclose

1

這將是清潔如果MySQL有一個UNPIVOT功能:

SELECT x.num, co.co - count(x.id) FROM 
    (SELECT count(id) as co FROM tab) as co, 
    (SELECT t.num, max(t.id) as 'id' FROM 
     (SELECT id, one as 'num' FROM tab 
     UNION 
     SELECT id, two as 'num' FROM tab 
     UNION 
     SELECT id, thr as 'num' FROM tab 
     UNION 
     SELECT id, fou as 'num' FROM tab 
     UNION 
     SELECT id, fiv as 'num' FROM tab) as t 
    GROUP BY t.num) as x 
    INNER JOIN 
    (SELECT id FROM tab) as y on x.id >= y.id 
GROUP BY x.num, co.co 
+0

這假設行ID正在增加,但不一定是順序的。 – Jodaka

+1

到目前爲止回答問題的唯一解決方案+1。然而,爲什麼還要使用'co':只需將表'y'上的連接標準顛倒爲'x.id <= y.id'(就此而論,直接加入'tab'而不是表'''子查詢)! http://sqlfiddle.com/#!2/dfba8/12 – eggyal