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命令。

CustomerId CustomerName CustomerAge CustomerGender
1 John 31 M
2 Amit 25 M
3 Annie 35 F
4 Tom 38 M
顾客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:

输出

CustomerId CustomerName CustomerAge CustomerGender
1 John 31 M
2 Amit 25 M
4 Tom 38 M
顾客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选择所有列

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模式中生成。

CustomerId CustomerName CustomerAge CustomerGender
1 John 31 M
2 Amit 25 M
4 Tom 38 M
顾客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行受影响

CustomerName CustomerAge
John 31
Amit 25
Tom 38
顾客姓名 客户年龄
约翰 31
阿米特 25
汤姆 38

SQL Select Into For Selected Columns

SQL选择到选定的列

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选择

sql选择_SQL选择成相关推荐

  1. sql选择题_SQL选择

    sql选择题 SQL or Structured Query Language is all about storing, retrieving and manipulating data in th ...

  2. Revit二次开发选择模型面生成新的三维视图

    觉得自己做的一些Revit二次开发的项目经验需要找个地方保存下来,不然以后碰到类似的又得去冲浪捞人家的结晶再整合,所以从今天起开始写CSDN博客啦 选择模型面生成新的三维视图 废话不多说,直接上代码! ...

  3. 如何选择PDF转换成PPT转换器?

    因PDF文档具有跨平台极强的印刷排版功能,因此得到了广泛的使用.现今有很多幻灯片也是使用PDF文档进行传播的,虽然PDF便于人们的阅读和交流,但由于自身所缺乏的编辑功能,而无法实现 PDF转换成PPT ...

  4. UG12.0数控加工选择边界,成链选项操作步骤

    UG12.0数控加工选择边界,成链选项操作步骤

  5. navicat循环执行上下两行相减sql语句_SQL太难?你离完全理解SQL就差这10步!

    - 点击上方"中国统计网"设置⭐星标不迷路!- 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语 ...

  6. sql简介_SQL表简介

    sql简介 In this article, we will learn the concept of SQL tables and then work on how we can create ta ...

  7. mysql创建多表视图sql语句_SQL语法(包括建库、建表、建视图、查询、增加、删除、修改)...

    SQL语法(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL-数据定义语言(CREATE,ALTER,DROP,DECLARE) DML-数据操纵语言(SELECT,DELETE ...

  8. 申请鸿蒙选择应用程序是什么,申请鸿蒙系统有一个应用选择怎么选择呢

    [分享交流] 申请鸿蒙系统有一个应用选择怎么选择呢 33804 电梯直达 huafen774590890 新学乍练 发表于 2020-12-18 22:19:17 来自:HUAWEI Mate 30 ...

  9. 鸿蒙申请敏感应用怎么选择,申请鸿蒙系统有一个应用选择怎么选择呢

    [分享交流] 申请鸿蒙系统有一个应用选择怎么选择呢 35694 电梯直达 huafen774590890 新学乍练 发表于 2020-12-18 22:19:17 来自:HUAWEI Mate 30 ...

最新文章

  1. golang中的defer
  2. Java 并发编程必须知道的七种锁类型以及应用
  3. 前端如何快速上手 Web 3D 游戏的开发
  4. vscode怎样导入数据_【Python开发】用VSCode+Jupyter notebook 编写 Python
  5. SpringBoot中fastJson返回数据展示null字段(响应值为null时字段被过滤)
  6. putty连接ubuntu虚拟机缓慢问题的解决
  7. oracle11g安装副武器类,oracle 11g 服务器类
  8. 《现代操作系统(中文第四版)》笔记 第一章 引论
  9. 学计算机逻辑思维能力测试题,逻辑思维能力测试题5道含答案
  10. 人工智能 2 级就来越塔来杀我,论文已被NeurIPS收录!
  11. h5 android 重力 晃动,H5案例分享:html5重力感应事件
  12. nvme装系统不能自引导_WinPE无法识别NVMe SSD硬盘,如何重装系统
  13. 什么品牌台灯最舒服?推荐最热门的护眼灯品牌排行
  14. 微信小程序剪切图片的功能
  15. sd卡tf卡进入spi模式
  16. SpringBoot Junit单元测试
  17. USB鼠标驱动开发流程
  18. 服务器×××上的MSDTC不可用
  19. java做服务端,FLASH做客户端交互总结
  20. sketch html尺寸,SketchSize一键生成多尺寸,为何你的设计效率如此之高?

热门文章

  1. 获取IP及判断IP是否在区间
  2. 解析:如何在 ASP.NET 中下载文件
  3. JS+CSS实现Dock menu(MacOS菜单导航效果)
  4. [转载] PyTorch简介
  5. [转载] AttributeError: ‘numpy.ndarray‘ object has no attribute ‘insert‘的解决方法
  6. 55)PHP,在html嵌套PHP写法
  7. 简单测试lambda和linq查找的性能
  8. 学习资料收集:计算机系统基础
  9. Pytorch---训练与测试时爆显存(out of memory)的一个解决方案(torch.cuda.empty_cache())
  10. 【RobotStudio学习笔记】(六)有效载荷