简体   繁体   English

如何在唯一选择中获得条目数和列名?

[英]How can i get number of entries and column name in a unique select?

I have the following table: 我有下表:

IdColumn   Name      Role   Status
  1        peter     Ope        0
  2        peter     Adm        1
  3        Jon       Ope        1
  4        Mac       Adm        1
  5        Jon       Adm        0
  6        peter     Sec        1

What I wanted to get is the column name and number of entries for each one in a single query. 我想要获得的是在单个查询中每个列的名称和条目数。

select Name, Count(*) from table where Status = 1; 

Exit example: 退出示例:

---------------
Name | Count()|
---------------
peter|  2     |
Jon  |  1     |
Mac  |  1     |
---------------

This is a basic group by query: 这是一个基本的group by查询group by

select Name, Count(*)
from table
where Status = 1
group by Name;

If you are going to use SQL, you should learn the basics. 如果要使用SQL,则应学习基础知识。 group by and join are definitely basics of the SQL language. group byjoin绝对是SQL语言的基础。

Use group by clause: 使用group by子句:

select Name, Count(*) from table where Status = 1 group by Name;
                                                   ^^^
                                             Add group by here

Query 询问

select count(*), Name 
from table 
where status=1 
group by Name

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

相关问题 选择列的唯一条目,然后按另一条目的唯一条目过滤 - Select unique entries of column and filter these by unique entries of another one 我如何 SQL 选择具有分组依据的唯一列? - How can i SQL Select unique column with group by? 如何从列中获得唯一的名称 - How can I get unique names from the column 如何从每个度量标准具有多个条目的表中选择列的总和? - How can I select the sum of a column from a table with multiple entries per metric? 如何使用SQL在表中选择带有数字名称的列 - How to select a column with a number name in a table with SQL SQL select 查询根据列值从两个外键表中获取唯一条目的总数 - SQL select query to get total unique entries from two foreign key tables based on a column value 如何选择包含非 ascii 字符的列名? - How can I select a column name that contains non ascii characters? 如何在 MySQL 中的 SELECT 期间使用 CONCAT 作为列名? - How can I use CONCAT during SELECT in MySQL as a column name? 如何获得名字,中间名和姓氏作为用户名,名称之间要有一个点,最后一个数字用于唯一的用户名? - How do I get first name, middle name and last name as username with a dot in between the names and a number at the end for a unique username? 我怎样才能得到员工人数的形成名称? - how can i get the name of formation with the number of employe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM