PhpPgAdmin并选择(max(num)from(select count(*)as num ...)作为计数)

时间:2013-11-22 06:35:24

标签: sql count max alias

我试试这个:

SELECT x.name 
FROM s x
WHERE (SELECT MAX(num) 
       FROM ((SELECT COUNT(*) AS num 
              FROM spj 
       WHERE n_post=x.n_post)>0) AS adf)

错误消息:

  

错误:“>

处或附近的语法错误      

第2行:...... CT计数(*)为来自spj的数字,其中n_post = x.n_post)> 0)为adf ...

然后我试试这个:

SELECT x.name 
FROM s x
WHERE (SELECT MAX(num) 
       FROM ((SELECT COUNT(*) AS num 
              FROM spj 
              WHERE n_post=x.n_post)) AS adf)

错误消息:

ERROR: argument of WHERE must be type boolean, not type bigint LINE 2: where (select max(num) from ((SELECT count(*) as num from sp..

有什么问题?求你帮帮我。

THX。我的代码:

SELECT x.name 
FROM s x
WHERE (SELECT MAX(num) 
       FROM (SELECT COUNT(*) AS num 
              FROM spj 
       WHERE x.n_post=n_post) AS adf)>0

我需要n_post,它具有表spj中的最大条目数,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

如果您需要n_post,其中包含表spj中的最大条目数,请尝试使用此查询。这是MySQL查询,它使用LIMIT关键字。如果您使用MsSQL,则使用SELECT TOP 1而不是LIMIT 1

SELECT x.name 
FROM s x
JOIN (SELECT n_post,COUNT(*) AS num FROM spj GROUP BY n_post) as y
     on x.n_post=y.n_post
ORDER BY y.num DESC 
LIMIT 1