T Sql 简明教程

T-SQL - SELECT Statement

SQL Server SELECT 语句用于从数据库表中获取数据,这些数据以结果表格的形式返回。这些结果表格被称为 result-sets

SQL Server SELECT statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets.

Syntax

以下是 SELECT 语句的基本语法 −

Following is the basic syntax of SELECT statement −

SELECT column1, column2, columnN FROM table_name;

其中,列 1、列 2…​ 是您要获取其值的表的字段。如果您想获取字段中提供的所有字段,则可以使用以下语法 −

Where, column1, column2…​are the fields of a table whose values you want to fetch. If you want to fetch all the fields available in the field, then you can use the following syntax −

SELECT * FROM table_name;

Example

考虑包含以下记录的 CUSTOMERS 表:

Consider the CUSTOMERS table having the following records −

ID  NAME       AGE       ADDRESS             SALARY
1   Ramesh     32        Ahmedabad           2000.00
2   Khilan     25        Delhi               1500.00
3   kaushik    23        Kota                2000.00
4   Chaitali   25        Mumbai              6500.00
5   Hardik     27        Bhopal              8500.00
6   Komal      22        MP                  4500.00
7   Muffy      24        Indore              10000.00

以下命令是一个示例,它将获取 CUSTOMERS 表中提供的客户的 ID、姓名和工资字段 −

Following command is an example, which would fetch ID, Name and Salary fields of the customers available in CUSTOMERS table −

SELECT ID, NAME, SALARY FROM CUSTOMERS;

以上命令将生成以下输出。

The above command will produce the following output.

ID  NAME          SALARY
1   Ramesh        2000.00
2   Khilan        1500.00
3   kaushik       2000.00
4   Chaitali      6500.00
5   Hardik        8500.00
6   Komal         4500.00
7   Muffy         10000.00

如果你想提取 CUSTOMERS 表的所有字段,那么使用以下查询 −

If you want to fetch all the fields of CUSTOMERS table, then use the following query −

SELECT * FROM CUSTOMERS;

上述代码将产生以下输出。

The above will produce the following output.

ID  NAME       AGE       ADDRESS              SALARY
1   Ramesh     32        Ahmedabad            2000.00
2   Khilan     25        Delhi                1500.00
3   kaushik    23        Kota                 2000.00
4   Chaitali   25        Mumbai               6500.00
5   Hardik     27        Bhopal               8500.00
6   Komal      22        MP                   4500.00
7   Muffy      24        Indore               10000.00