sql选择_SQL选择成

sql选择

SQL选择成 (SQL Select Into)

SQL SELECT INTO operator is a very useful operator when it comes to data migration. SELECT INTO is used for the following two purposes.

对于数据迁移,SQL SELECT INTO运算符是非常有用的运算符。 SELECT INTO用于以下两个目的。

  1. SELECT INTO copies data from all the columns of one table to new table.

    SELECT INTO将数据从一个表的所有列复制到新表。
  2. SELECT INTO copies data only from selected columns of one table to a new table.

    SELECT INTO仅将数据从一个表的选定列复制到新表。

We will try to understand both the usage in detail in below-mentioned sections.

我们将在以下各节中尝试详细了解这两种用法。

新表中的所有列 (All Columns in New Table)

We will try to understand the syntax for using SQL SELECT INTO for copying all the columns from one table to a new table.
Select Into Syntax

我们将尝试了解使用SQL SELECT INTO将所有列从一个表复制到新表的语法。
选择进入语法

SELECT * INTO new_table FROM old_table_name WHERE condition;

In the syntax above all the columns data is retrieved based on the WHERE clause and a new table is created with the name provided.

在以上语法中,所有列数据均基于WHERE子句检索,并使用提供的名称创建新表。

Let’s consider the following Customer Table to understand SELECT INTO command for copying all the columns data based on a condition.

让我们考虑以下客户表,以了解用于根据条件复制所有列数据的SELECT INTO命令。

CustomerIdCustomerNameCustomerAgeCustomerGender
1John31M
2Amit25M
3Annie35F
4Tom38M
顾客ID 顾客姓名 客户年龄 客户性别
1个 约翰 31 中号
2 阿米特 25 中号
3 安妮 35 F
4 汤姆 38 中号

Scenario: Create a new table from the Customer table where customer gender is male.

场景 :从“客户”表中创建一个新表,其中“客户”为男性。

SELECT * INTO MaleCustomer FROM Customer WHERE CustomerGender = 'M';

Output:

输出

CustomerIdCustomerNameCustomerAgeCustomerGender
1John31M
2Amit25M
4Tom38M
顾客ID 顾客姓名 客户年龄 客户性别
1个 约翰 31 中号
2 阿米特 25 中号
4 汤姆 38 中号

We can also copy data of all the columns in a new table in a different database using IN clause from an old table.

我们还可以使用旧表中的IN子句将新表中所有列的数据复制到其他数据库中。

SQL Select Into For All Columns

SQL Select Into For All Columns

SQL选择所有列

After SQL Select Into For All Columns

After SQL Select Into For All Columns

在SQL之后选择到所有列

Syntax:

语法

SELECT * INTO new_table IN external_db FROM old_table_name WHERE condition

Let’s try to understand the above-mentioned syntax using below mentioned example.

让我们尝试使用下面提到的示例来理解上述语法。

Scenario: Create a table in the backup database from customer table where customer gender is male.

场景 :从客户表中的客户性别为男性的备份数据库中创建一个表。

SELECT * INTO MaleCustomer IN 'backup.mdb' FROM Customer WHERE CustomerGender = 'M';

Output: 3 rows affected

输出 :3行受影响

The newly generated table will look as shown below and will be generated in backup.mdb schema.

新生成的表如下所示,并将在backup.mdb模式中生成。

CustomerIdCustomerNameCustomerAgeCustomerGender
1John31M
2Amit25M
4Tom38M
顾客ID 顾客姓名 客户年龄 客户性别
1个 约翰 31 中号
2 阿米特 25 中号
4 汤姆 38 中号

新表中的选择性栏 (Selective Columns in New Table)

We will try to understand the syntax for using SQL SELECT INTO for copying selective columns from one table to a new table.

我们将尝试了解使用SQL SELECT INTO将选择性列从一个表复制到新表的语法。

Syntax:

语法

SELECT column_name(s) INTO new_table FROM old_table_name WHERE condition;

In the syntax above selective columns data is retrieved based on the WHERE clause and a new table is created with the name provided.

在上面的语法中,基于WHERE子句检索选择性列数据,并使用提供的名称创建新表。

Scenario: Create a new table from Customer table with only customer name and age where customer gender is male.

方案 :从“客户”表创建一个新表,其中仅包含客户名称和年龄(客户性别为男性)。

SELECT CustomerName, CustomerAge INTO TestCustomer FROM Customer WHERE CustomerGender = 'M';

Output: 3 rows affected

输出 :3行受影响

CustomerNameCustomerAge
John31
Amit25
Tom38
顾客姓名 客户年龄
约翰 31
阿米特 25
汤姆 38
SQL Select Into For Selected Columns

SQL Select Into For Selected Columns

SQL选择到选定的列

After SQL Select Into For Selected Columns

After SQL Select Into For Selected Columns

在SQL之后选择进入选定的列

We can also create an empty table which will have the same schema of the old table by providing a where condition which does not return any value.

我们还可以通过提供不返回任何值的where条件来创建一个与旧表具有相同架构的空表。

SELECT * INTO NewCustomer FROM Customer WHERE 1=0;

The above query will result in a table with the name as NewCustomer which will have the same schema as Customer table.

上面的查询将导致一个名为NewCustomer的表,该表具有与Customer表相同的架构。

MySQL选择进入 (MySQL Select Into)

MySQL doesn’t support Select Into clause. So we can use below query to create a table by selecting data from another table.

MySQL不支持Select Into子句。 因此,我们可以使用下面的查询通过从另一个表中选择数据来创建表。

CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;

NOTE: In the above query, AS is optional.

注意:在上面的查询中,AS是可选的。

翻译自: https://www.journaldev.com/24313/sql-select-into

sql选择

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值