博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql语句count_带COUNT()函数SQL SELECT语句
阅读量:2533 次
发布时间:2019-05-11

本文共 4254 字,大约阅读时间需要 14 分钟。

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?)

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

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

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	city1	100	Pune2	100	Satara3	65	Pune4	97	Mumbai5	12	USA


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

counts the total number of rows present in the database.

计算数据库中存在的总行数。

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 to create a Table and to input data to the database.

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

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	city1	100	Pune2	100	Satara3	65	Pune4	97	Mumbai5	12	USA e


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

SQL SELECT COUNT() can be clubbed with .

SQL SELECT COUNT()可以与 。

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 InfoWHERE 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 helps exclude the redundant data and displays only the unique values from the selected column.

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

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 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和以在选择数据以及按特定列值对数据行进行分组之前添加条件。

Example:

例:

SELECT city, COUNT(Cost)FROM InfoGROUP BY cityHAVING 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有关的更多此类帖子,请访问 。



参考资料 (References)

翻译自:

sql语句count

转载地址:http://toqzd.baihongyu.com/

你可能感兴趣的文章
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>