简体   繁体   English

mysql查询(多个计数)

[英]mysql query (multiple counts)

With this query: 使用此查询:

SELECT abc, def, COUNT(*) as c 
FROM xpto 
GROUP BY abc, def
ORDER BY abc, c DESC

I have this result: 我有这个结果:

xpto

abc | def | c

x | c_1 | 8
...
y | a_2 | 4
y | a_1 | 2
y | a_3 | 1
…
z | b_2 | 7
z | b_1 | 3
...

I wish to have this result (ordered by number of entries for each abc and field c): 我希望得到这个结果(按每个abc和字段c的条目数排序):

y | a_2 
y | a_1
y | a_3

z | b_2
z | b_1

x | c_1

Can a SQL guru help me with this task? SQL专家可以帮助我完成此任务吗? I've seen this example SQL Help: Counting Rows in a Single Query With a Nested SELECT , is this a good solution (nested select), or there is no other way to do? 我已经看过这个示例SQL帮助:使用嵌套SELECT的单个查询中的行计数 ,这是一个好的解决方案(嵌套选择),还是没有其他方法可做?

Thanks in advance 提前致谢

Use a nested query: 使用嵌套查询:

SELECT abc, def 
FROM (
  SELECT abc, def, COUNT(*) as c 
  FROM xpto 
  GROUP BY abc, def
) alias
ORDER BY abc, c DESC

(from comment) The challenge is not order abc alphabetically but by number of entries with the same value. (来自注释)挑战不是按字母abc顺序排序,而是按具有相同值的条目数排序。 I edited the example in question to better understand the ultimate goal. 我编辑了相关示例,以更好地理解最终目标。

OK, then just change the order of columns in the ORDER BY clause: OK,然后只需更改ORDER BY子句中的列顺序即可:

SELECT abc, def, c
FROM (
  SELECT abc, def, COUNT(*) as c 
  FROM xpto 
  GROUP BY abc, def
) alias
ORDER BY c DESC, abc;

The above query gives aggregate valuse (ie Unique rows - one row per each pair of values). 上面的查询给出了合计值(即唯一行-每对值一对)。

If you wish to list all rows from the table ordered by number of entries, try this query: 如果您希望列出表中按条目数排序的所有行,请尝试以下查询:

SELECT abc, def, 
       ( SELECT COUNT(*) FROM xpto x1
         WHERE ( x.abc, x.def ) = (x1.abc, x1.def)
        ) as c
FROM xpto x
ORDER by c desc, abc
; 

This query displays columns abc, def + count. 该查询显示abc,def + count列。 If you want to display only abc + def, without the value of count, then try this query: 如果只想显示abc + def,而没有count的值,请尝试以下查询:

SELECT abc, def
FROM xpto x
ORDER by  (  SELECT COUNT(*) FROM xpto x1
             WHERE x.abc = x1.abc AND x.def = x1.def
           ) desc, 
           abc
;

Look at sqlfiddle demo that demonstrates these 3 queries. 看一下展示这3个查询的sqlfiddle演示

Pay attention to the condition used in the second query (in the dependent subquery): 请注意第二个查询(从属子查询)中使用的条件:

WHERE ( x.abc, x.def ) = (x1.abc, x1.def)

This syntax is complaint with ANSI SQL, however may not work on some older version of MySQL. ANSI SQL抱怨这种语法,但是可能不适用于某些旧版本的MySQL。 In that case, change this condition to its equivalent version: 在这种情况下,请将此条件更改为等效版本:

WHERE x.abc = x1.abc AND x.def = x1.def

Check this query 检查此查询

SELECT abc, def, COUNT(*) as c,
  (SELECT COUNT(1) FROM (
        SELECT abc, def, COUNT(*) as c
        FROM xpto A
        GROUP BY abc, def
    ) B
  GROUP BY ABC
  HAVING C.abc=B.abc
  ) ic
FROM xpto C
GROUP BY abc, def
ORDER BY ic DESC, c DESC

This may not be the best way to achieve this, but it works (Check this SQL Fiddle ) 这可能不是实现此目的的最佳方法,但它可以工作(请检查此SQL Fiddle

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

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