简体   繁体   English

mysql-如何查询表,按名称分组和同一表中的标题计数?

[英]mysql - how to query a table, group by name and count of titles in the same table?

Trying to wrap my head around this query, and have tried grouping, but no luck: 试图绕过这个查询,尝试分组,但是没有运气:

If I have a table: 如果我有一张桌子:

name         title         (some other fields)
John Doe     Engineer      ...
John Doe     Tech          ...
John Doe     Tech          ...
Frank Smith  Tech          ...
Frank Smith  Tech          ...

I need a query that would result in: 我需要一个查询,将导致:

name         title      count(title)
John Doe     Engineer   1
John Doe     Tech       2
Frank Smith  Tech       2 

Have tried using group by name and title, but it seems to just group one or the other, giving me the count of either total engineers (1) and techs (4), or total clients (2), but I need total titles BY name. 曾尝试使用名称和标题分组,但似乎只是将一个或另一个分组,这给了我工程师总数(1)和技术人员(4)或客户总数(2)的数量,但我需要标题总数BY名称。

Any suggestions? 有什么建议么?

Don't know why wouldn't this work: 不知道为什么这样行不通:

SELECT name, title, COUNT(*) Titles
FROM YourTable
GROUP BY name, title

try this 尝试这个

 SELECT name , title , count(*) count
 FROM Table1
 GROUP BY name , title
 ORDER BY count

DEMO SQLFIDDLE 演示SQLFIDDLE

SELECT NAME, TITLE, COUNT(*) FROM BOOKS GROUP BY NAME, TITLE

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

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