博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Select语句:Select Distinct,Select Into,Insert into等示例
阅读量:2526 次
发布时间:2019-05-11

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

Select和From子句 (Select and From clauses)

The SELECT part of a query is normally to determine which columns of the data to show in the results. There are also options you can apply to show data that is not a table column.

查询的SELECT部分​​通常是确定要在结果中显示数据的哪几列。 您还可以应用其他选项来显示不是表列的数据。

This example shows three columns selected from the “student” table and one calculated column. The database stores the studentID, FirstName, and LastName of the student. We can combine the First and the Last name columns to create the FullName calculated column.

本示例显示了从“学生”表中选择的三列和一个计算出的列。 该数据库存储该学生的studentID,FirstName和LastName。 我们可以组合名字和姓氏列来创建FullName计算列。

select studentID, FirstName, LastName, FirstName + ' ' + LastName as FullNamefrom student;
+-----------+-------------------+------------+------------------------+| studentID | FirstName         | LastName   | FullName               |+-----------+-------------------+------------+------------------------+|         1 | Monique           | Davis      | Monique Davis          ||         2 | Teri              | Gutierrez  | Teri Gutierrez         ||         3 | Spencer           | Pautier    | Spencer Pautier        ||         4 | Louis             | Ramsey     | Louis Ramsey           ||         5 | Alvin             | Greene     | Alvin Greene           ||         6 | Sophie            | Freeman    | Sophie Freeman         ||         7 | Edgar Frank "Ted" | Codd       | Edgar Frank "Ted" Codd ||         8 | Donald D.         | Chamberlin | Donald D. Chamberlin   ||         9 | Raymond F.        | Boyce      | Raymond F. Boyce       |+-----------+-------------------+------------+------------------------+9 rows in set (0.00 sec)

*As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

*与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。

I hope this at least gives you enough to get started.

我希望这至少能给您足够的入门。

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

SQL选择不同的语句 (SQL Select Distinct Statement)

介绍 (Introduction)

This keyword allows us to get lists of unique values in a column. This guide will demonstrate that.

此关键字使我们能够获取列中唯一值的列表。 本指南将证明这一点。

完整显示学生表中的数据 (Full display of the data in the student table)

USE fcc_sql_guides_database;SELECT studentID, FullName, sat_score, programOfStudy, rcd_Created, rcd_Updated FROM student;
+-----------+------------------------+-----------+------------------+---------------------+---------------------+| studentID | FullName               | sat_score | programOfStudy   | rcd_Created         | rcd_Updated         |+-----------+------------------------+-----------+------------------+---------------------+---------------------+|         1 | Monique Davis          |       400 | Literature       | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         2 | Teri Gutierrez         |       800 | Programming      | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         3 | Spencer Pautier        |      1000 | Programming      | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         4 | Louis Ramsey           |      1200 | Programming      | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         5 | Alvin Greene           |      1200 | Programming      | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         6 | Sophie Freeman         |      1200 | Programming      | 2017-08-16 15:34:50 | 2017-08-16 15:34:50 ||         7 | Edgar Frank "Ted" Codd |      2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 ||         8 | Donald D. Chamberlin   |      2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 ||         9 | Raymond F. Boyce       |      2400 | Computer Science | 2017-08-16 15:35:33 | 2017-08-16 15:35:33 |+-----------+------------------------+-----------+------------------+---------------------+---------------------+9 rows in set (0.00 sec)

获取研究领域清单 (Get list of fields of study)

SELECT DISTINCT programOfStudy FROM student;
+------------------+| programOfStudy   |+------------------+| Literature       || Programming      || Computer Science |+------------------+3 rows in set (0.00 sec)

As with all of these SQL things there is MUCH MORE to them than what’s in this introductory guide.

与所有这些SQL事物一样,它们比本入门指南中的内容要多得多。

I hope this at least gives you enough to get started.

我希望这至少能给您足够的入门。

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

SQL选择入语句 (SQL Select into Statement)

The SELECT INTO statement is a query that allows you to create a new table and populate it with the result set of a SELECT statement. To add data to an existing table, see the statement instead.

SELECT INTO语句是一个查询,使您可以创建一个表,并使用SELECT statement的结果集填充该SELECT statement 。 要将数据添加到现有表中,请参阅语句。

SELECT INTO can be used when you are combining data from several tables or views into a new table.1 The original table is not affected.

将多个表或视图中的数据合并到一个新表中时,可以使用SELECT INTO1原始表不受影响。

The general syntax is:

通用语法为:

SELECT column-names  INTO new-table-name  FROM table-name WHERE EXISTS       (SELECT column-name         FROM table-name        WHERE condition)

This example shows a set of a table that was “copied” from the “Supplier” table to a new one called SupplierUSA which holds the set related to the column country of value ‘USA’.

此示例显示了一个表集,该表集是从“供应商”表中“复制”到一个名为SupplierUSA的新表中的,该表中包含与值“ USA”的列国家有关的集合。

SELECT * INTO SupplierUSA  FROM Supplier WHERE Country = 'USA';

Results: 4 rows affected 2

结果 :4行受影响2

IDCompanyNameContactNameCityCountryPhone2New Orleans Cajun DelightsShelley BurkeNew OrleansUSA(100) 555-48223Grandma Kelly’s HomesteadRegina MurphyAnn ArborUSA(313) 555-573516Bigfoot BreweriesCheryl SaylorBendUSANULL19New England Seafood CanneryRobb MerchantBostonUSA(617) 555-3267

IDCompanyNameContactNameCityCountryPhone2New Orleans Cajun DelightsShelley BurkeNew OrleansUSA(100)555-48223Grandma Kelly's HomesteadRegina MurphyAnn ArborUSA(313)555-573516Bigfoot BrewerysCheryl SaylorBendUSANULL19New England Seafood CanneryRobb MerchantBostonUSA(617)

Please see the manual for your database manager and have fun trying different options yourself.

请参阅数据库管理员的手册,并尝试自己尝试其他选项,这很有趣。

SQL插入语句 (SQL Insert into Statement)

To insert a record in a table you use the INSERT INTO statement.

要在表中插入记录,请使用INSERT INTO语句。

You can do it in two ways, if you want to insert values only in some columns, you have to list their names including all mandatory columns. The syntax is:

您可以通过两种方式进行操作,如果只想在某些列中插入值,则必须列出其名称,包括所有必填列。 语法为:

INSERT INTO table_name (column1, column2, column3, ...)VALUES (value1, value2, value3, ...);

The other way is inserting values to all columns in the table, it is not necessary to specify the columns names. The syntax is:

另一种方法是将值插入表中的所有列,而不必指定列名称。 语法为:

INSERT INTO table_name VALUES (value1, value2, value3, ...);

Here’s an example inserting a record in the table Person in both ways:

这是一个以两种方式在表Person中插入记录的示例:

INSERT INTO PersonVALUES (1, ‘John Lennon’, ‘1940-10-09’, ‘M’);

And

INSERT INTO Person(Id, Name, DateOfBirth, Gender)VALUES (1, ‘John Lennon’, ‘1940-10-09’, ‘M’);

Some SQL versions (for example, MySQL) support inserting multiple rows at once. For example:

某些SQL版本(例如MySQL)支持一次插入多行。 例如:

INSERT INTO Person(Id, Name, DateOfBirth, Gender)VALUES (1, ‘John Lennon’, ‘1940-10-09’, ‘M’), (2, ‘Paul McCartney’, ‘1942-06-18’, ‘M’),(3, ‘George Harrison’, ‘1943-02-25’, ‘M’), (4, ‘Ringo Starr’, ‘1940-07-07’, ‘M’)

Note that the entire original query remains intact - we simple add on data rows encloded by paranthesis and separated by commas.

请注意,整个原始查询保持不变-我们简单地添加了用括号括起来并用逗号分隔的数据行。

SQL插入Select语句 (SQL Insert into Select Statement)

You can insert records in a table using data that are already stored in the database. This is only a copy of data and it doesn’t affect the origin table.

您可以使用数据库中已经存储的数据在表中插入记录。 这只是数据的副本,不会影响原始表。

The INSERT INTO SELECT statement combines INSERT INTO and SELECT statements and you can use any conditions you want. The syntax is:

INSERT INTO SELECT语句结合了INSERT INTOSELECT语句,您可以使用所需的任何条件。 语法为:

INSERT INTO table2 (column1, column2, column3, ...)SELECT column1, column2, column3, ...FROM table1WHERE condition;

Here is an example that inserts in the table Person all the male students from the table Students.

这是一个示例,在表Person中插入了Students表中的所有男学生。

INSERT INTO Person(Id, Name, DateOfBirth, Gender)SELECT Id, Name, DateOfBirth, GenderFROM StudentsWHERE Gender = ‘M’

其他SQL资源: (Other SQL resources:)

翻译自:

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

你可能感兴趣的文章
《项目架构那点儿事》——快速构建Junit用例
查看>>
{"errmsg":"invalid weapp pagepath hint: [IunP8a07243949]","errcode":40165}微信的坑
查看>>
DB2V9.5数据库使用pdf
查看>>
Java Bigdecimal使用
查看>>
SQL注入之绕过WAF和Filter
查看>>
jquery validate使用方法
查看>>
DataNode 工作机制
查看>>
windows系统下安装MySQL
查看>>
错误提示总结
查看>>
实验二+070+胡阳洋
查看>>
Linux IPC实践(3) --具名FIFO
查看>>
Qt之模拟时钟
查看>>
第一次接触安卓--记于2015.8.21
查看>>
(转)在分层架构下寻找java web漏洞
查看>>
mac下多线程实现处理
查看>>
C++ ifstream ofstream
查看>>
跟初学者学习IbatisNet第四篇
查看>>
seL4环境配置
查看>>
Git报错:insufficient permission for adding an object to repository database .git/objects
查看>>
ajax跨域,携带cookie
查看>>