2015-05-14 33 views
4

使用SELECT內COUNT我有這樣的看法:如何在MYSQL

MyView的:(ID,百分比)

我要讓另一種觀點,至極將是這樣的:

LASTVIEW: (lastID,lastPR,計數器)

和「evrey line」中的「counter」我想知道money id的百分比大於這一行的百分比。所以我嘗試過:

CREATE VIEW LASTVIEW(lastID, lastPR, counter) AS 
SELECT id AS lastID, percent AS lastPR 
COUNT (SELECT id FROM myVIEW WHERE percent < lastPR) AS counter, 
FROM myVIEW; 
+0

使用您的其他查詢在你的'FROM'子查詢。閱讀[this](https://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html) – adarsh

回答

3

你幾乎在那裏。試試這個:

SELECT id AS lastID, percent AS lastPR, (SELECT Count(id) 
FROM myVIEW bigger 
WHERE bigger.percent > myv.percent) AS counter 
FROM myVIEW myv 
+0

它的工作原理!謝謝! 但我有一個奇怪的分段錯誤(核心傾倒)! – Atheel

+0

你知道爲什麼是細分嗎? – Atheel

+0

我不知道數據庫系統如何得到分段錯誤。你使用自己的程序來運行查詢嗎?如果是這樣,我們需要檢查你的代碼。 – Tim3880

0

使用下面的select查詢創建視圖

SELECT lastID,lastPR,counter 
FROM (SELECT A.id AS lastID,A.percent AS lastPR,count(B.id) AS counter 
FROM myVIEW A,myVIEW B WHERE B.percent<A.percent 
GROUP BY A.id,A.percent)