本文共 4254 字,大约阅读时间需要 14 分钟。
sql语句count
Hey, folks! In this article, we will be focusing on SQL SELECT statement along with COUNT() function.
嘿伙计! 在本文中,我们将重点介绍SQL SELECT语句以及COUNT()函数 。
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
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
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()的工作原理后,现在让我们通过示例来了解与之相关的不同变体。
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
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
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
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
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
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有关的更多此类帖子,请访问 。
翻译自:
sql语句count
转载地址:http://toqzd.baihongyu.com/