sql语句count_带COUNT()函数SQL SELECT语句

sql语句count

Hey, folks! In this article, we will be focusing on SQL SELECT statement along with COUNT() function.

嘿伙计! 在本文中,我们将重点介绍SQL SELECT语句以及COUNT()函数



什么是SQL SELECT语句? (What is SQL SELECT statement?)

SQL SELECT statement helps us select and display the data values from the particular table of the database.

SQL SELECT语句帮助我们从数据库的特定表中选择并显示数据值。

Syntax:

句法:


SELECT columns 
FROM Table-name;

Example:

例:


SELECT * from Info;

SELECT * statement helps select all the data values from the provided table.

SELECT *语句有助于从提供的表中选择所有数据值。

Output:

输出:


id	Cost	city
1	100	Pune
2	100	Satara
3	65	Pune
4	97	Mumbai
5	12	USA


什么是SQL COUNT()函数? (What is SQL COUNT() function?)

SQL COUNT() function counts the total number of rows present in the database.

SQL COUNT()函数计算数据库中存在的总行数。

Syntax:

句法:


COUNT(column-name)

Example:

例:


SELECT Count(City)
from Info;

In this example, we have displayed the count of all the data rows under the column – ‘city’ of table – ‘Info’.

在此示例中,我们在列–表的“城市” –“信息”下显示了所有数据行的计数。

Output:

输出:


5


带COUNT()函数SQL SELECT语句的变体 (Variations of SQL SELECT statement with COUNT() function)

You can use the SQL SELECT statement with the COUNT() function to select and display the count of rows in a table of a database.

您可以将SQL SELECT语句与COUNT()函数一起使用,以选择和显示数据库表中的行数。

Along with this, we can club SQL SELECT statement with COUNT() function in various different ways.

除此之外,我们还可以通过各种不同方式将SQL SELECT语句与COUNT()函数结合使用。

Having understood the working of SQL SELECT COUNT(), let us now understand different variations associated with the same through examples.

了解了SQL SELECT COUNT()的工作原理后,现在让我们通过示例来了解与之相关的不同变体。



带COUNT()函数SQL SELECT的示例 (Examples of SQL SELECT with COUNT() function)

To display the variations in SQL SELECT COUNT(), we have used SQL CREATE query to create a Table and SQL INSERT query to input data to the database.

为了显示SQL SELECT COUNT()中的变化,我们使用了SQL CREATE查询来创建表,并使用SQL INSERT查询来将数据输入数据库。

We will be using the below table and its data in the further examples.

在其他示例中,我们将使用下表及其数据。


create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 100, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
select * from Info;

Output:

输出:


id	Cost	city
1	100	Pune
2	100	Satara
3	65	Pune
4	97	Mumbai
5	12	USA e


1.带WHERE子句SQL SELECT COUNT (1. SQL SELECT COUNT with WHERE clause)

SQL SELECT COUNT() can be clubbed with SQL WHERE clause.

SQL SELECT COUNT()可以与SQL WHERE子句结合使用

Using the WHERE clause, we have access to restrict the data to be fed to the COUNT() function and SELECT statement through a condition.

使用WHERE子句,我们可以通过条件来限制要馈入COUNT()函数和SELECT语句的数据。

Example:

例:


SELECT COUNT(city)
FROM Info
WHERE Cost>50;

Output:

输出:


3


2. SQL SELECT COUNT(*)函数 (2. SQL SELECT COUNT(*) function)

SQL SELECT statement can be used along with COUNT(*) function to count and display the data values.

SQL SELECT语句可与COUNT(*)函数一起使用以计数和显示数据值。

The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

COUNT(*)函数表示表中所有行的计数(包括NULL和NON-NULL值)。

Example:

例:


SELECT COUNT(*)
FROM Info;

Output:

输出:


5


3.带DISTINCT子句SQL SELECT COUNT (3. SQL SELECT COUNT with DISTINCT clause)

The DISTINCT clause helps exclude the redundant data and displays only the unique values from the selected column.

DISTINCT子句有助于排除冗余数据,并仅显示所选列中的唯一值。

SQL SELECT COUNT() function can be used along with DISTINCT clause to count and display the number of rows representing unique(non-repeated) values.

SQL SELECT COUNT()函数可与DISTINCT子句一起使用,以计数和显示表示唯一(非重复)值的行数。

Example:

例:


SELECT COUNT(DISTINCT Cost)
FROM Info;

Output:

输出:


4


4.具有HAVING和GROUP BY子句SQL SELECT COUNT (4. SQL SELECT COUNT with HAVING and GROUP BY clause)

SQL SELECT COUNT() function can be clubbed with GROUP BY and HAVING clause to add conditions before the selection of data as well as grouping of data rows by a particular column value.

SQL SELECT COUNT()函数可以与GROUP BY和HAVING子句结合使用,以在选择数据以及按特定列值对数据行进行分组之前添加条件。

Example:

例:


SELECT city, COUNT(Cost)
FROM Info
GROUP BY city
HAVING COUNT(Cost)>1;

Output:

输出:


city	COUNT(Cost)
Pune	2


结论 (Conclusion)

By this, we have come to an end of this topic. Please feel free to comment below in case you come across any doubt.

至此,我们结束了本主题。 如果您有任何疑问,请随时在下面发表评论。

For more such posts related to SQL, please do visit SQL JournalDev.

有关与SQL有关的更多此类帖子,请访问SQL JournalDev



参考资料 (References)

翻译自: https://www.journaldev.com/41167/sql-select-statement-with-count

sql语句count

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值