简体   繁体   English

MySQl:如何在子查询上使用Count?

[英]MySQl : How do I use Count on a Sub-Query?

I've got a MySQL statement that selects a name and also makes a ranking. 我有一个MySQL语句,该语句选择一个名称并进行排名。

  SELECT t.name,                        
         (SELECT COUNT(*)
            FROM my_table1 z
           WHERE z.type LIKE '%Blue%' 
             AND t.type  LIKE '%Blue%'
             AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS  rank                     
    FROM my_table1 t, my_table2 d
   WHERE d.name = t.name
     AND t.status != 'unknown'
     AND t.type = 'Blue'
     AND d.area_served = '$area_id'                 
ORDER BY rank ASC

But, I also need to know out of how many the rank is calculated. 但是,我还需要知道要计算多少等级。 So for example, ranked #4 out of X. 因此,例如,在X中排名第4。

How do I count the total number of rows in the ranking sub-query? 如何计算排名子查询中的总行数? I need the count for this bit: 我需要这一点的计数:

(SELECT COUNT(*)
    FROM my_table1 z
    WHERE z.type LIKE '%Blue%' AND  t.type  LIKE '%Blue%'
    AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS  rank

Thank you. 谢谢。

-Laxmidi -Laxmidi

You can add one more subquery - it will be the same as the existing, but without AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4) condition: 您可以再添加一个子查询-与现有子查询相同,但不使用AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)条件:

 SELECT t.name,                        
     (SELECT COUNT(*)
        FROM my_table1 z
       WHERE z.type LIKE '%Blue%' 
         AND t.type  LIKE '%Blue%'
         AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS  rank,
      // new subquery
    (SELECT COUNT(*)
        FROM my_table1 z
       WHERE z.type LIKE '%Blue%' 
         AND t.type  LIKE '%Blue%') as max_rank
FROM my_table1 t, my_table2 d   
WHERE d.name = t.name
 AND t.status != 'unknown'
 AND t.type = 'Blue'
 AND d.area_served = '$area_id'                 
ORDER BY rank ASC

You can use the same subselect without the score comparison: 您可以使用相同的子选择而不进行分数比较:

SELECT t.name,                        
         (SELECT COUNT(*)
            FROM my_table1 z
           WHERE z.type LIKE '%Blue%' 
             AND t.type  LIKE '%Blue%'
             AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS  rank,                 
         (SELECT COUNT(*)
            FROM my_table1 z
           WHERE z.type LIKE '%Blue%' 
             AND t.type  LIKE '%Blue%') AS rankOutOf
    FROM my_table1 t, my_table2 d
   WHERE d.name = t.name
     AND t.status != 'unknown'
     AND t.type = 'Blue'
     AND d.area_served = '$area_id'   

The rankOutOf column returns the number of candidates considered in the ranking query. rankOutOf列返回排名查询中考虑的候选数。

I'm not sure if I understand, but I think you should include FOUND_ROWS() in the subquery. 我不确定是否可以理解,但是我认为您应该在子查询中包含FOUND_ROWS()。

(SELECT COUNT(*), FOUND_ROWS() FROM my_table1 z WHERE z.type LIKE '%Blue%' AND t.type LIKE '%Blue%' AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS rank, number (从my_table1 z中选择COUNT(*),FOUND_ROWS(),z.type LIKE'%Blue%'和t.type LIKE'%Blue%'AND(z.score1 + z.score2 + z.score3 + z.score4) > =(t.score1 + t.score2 + t.score3 + t.score4))AS排名,编号

You can find more information here: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows 您可以在这里找到更多信息: http : //dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows

I'm having a little trouble understanding your question, but I'll take a stab at it. 我在理解您的问题时遇到了一些麻烦,但我会采取行动。

This part gets you the specific rank number (such as 4): 这部分为您提供特定的等级编号(例如4):

(SELECT COUNT(*)
    FROM my_table1 z
    WHERE z.type LIKE '%Blue%' AND  t.type  LIKE '%Blue%'
    AND (z.score1+ z.score2 + z.score3 + z.score4) >= (t.score1+ t.score2 + t.score3 + t.score4)) AS  rank

So in order to find the total number of rows from that subquery, you should just need to remove your WHERE clause. 因此,为了找到该子查询的总行数,您只需要删除WHERE子句即可。 I'm not sure if you need to remove everything in the WHERE clause though, maybe just the types, or just the scores? 我不确定是否需要删除WHERE子句中的所有内容,也许只是类型,还是分数?

If you have multiple rows that you want to be grouped together, I would use GROUP BY and then use COUNT as necessary. 如果您要将多个行分组在一起,我将使用GROUP BY ,然后根据需要使用COUNT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM