如何在MYSQL中使用COUNT里面的SELECT

时间:2015-05-14 19:03:50

标签: mysql sql view count

我有这个观点:

myVIEW :( id,percent)

我想提出另一种观点,这将是这样的:

LASTVIEW :( lastID,lastPR,counter)

并且在"计数器",在evrey系列中我想知道如何赚钱比这条线的百分比更大。所以我试过了:

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;

2 个答案:

答案 0 :(得分:5)

你几乎就在那里。试试这个:

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

答案 1 :(得分:0)

使用以下选择查询创建视图

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)