目录

一、DQL

1.1、语法结构

1.2、着重号

1.3、SQL简单使用

二、别名查询

2.1、给字段名取别名

2.2、给表取别名

三、去重查询(distinct)

四、单表查询

4.1、算术运算符

4.2、比较运算符

4.3、逻辑运算符

4.4、范围和集合

4.5、模糊查询

4.6、统计查询

4.7、分页查询(limit)

4.8、分组查询

4.9、排序查询

五、多表查询

5.1、笛卡尔积

5.2、关联查询

5.3、内连接

5.4、外连接

5.4.1、左外连接

5.4.2、右外连接

5.4.3、全外连接(union)

5.5、自连接

六、子查询

6.1、where 型子查询

6.2、from 型子查询

6.3、exists 型子查询

6.4、复制表子查询

6.4.1、复制表结构

6.4.2、复制表结构和数据

6.4.3、复制表结构+数据

6.4.4、跨数据库复制

6.4.5、复制表中部分字段和数据

6.4.6、在创建表的同时定义表的字段


一、DQL

DQL 是数据查询语言,用于查询数据,它是我们在真正的开发中使用最多的一种。而我们项目用 得最多的也是查询。而在 SQL 中,DQL所占比例也是最大的,结构也是最复杂的。

1.1、语法结构

select 字段列表
from 表名或视图
[where 条件]
[group by 字段列表
[having 分组条件]]
[order by 字段 [asc | desc]]
[limit m, n]

语法说明:

(1)使用 select 关键字来做查询;

(2)字段列表是使用英文逗号来进行分割 ;

(3)from关键字后跟表的,可以是多个表名称或视图名称;

(4)group by:用于分组查询;

(5)having:用于结分组查询时进行过滤——条件的筛选;

(6)order by:用于数据显示时的排序;

(7)asc:表示长序,它是默认值;

(8)desc:表示降序;

(9)limit:用于分页显示,它有两个参数,第一个参数是起始值,第二个参数是显示的数量。

1.2、着重号

有些字段或表名可以使用了数据库的关键字或保留,这时我们就可以使用着重号(也叫反引号``)来进行着重标明

示例:

select `name` from t_stu;

1.3、SQL简单使用

示例一:查询员工表中所有行所有列

select * from t_employee;
使用 * 号来表示表中所有的字段,即查询所有行所有列的数据。

注意:在实际开发中,不建议在SQL中使用 *

示例二:查询部分字段

select eid,ename,gender from t_employee;

示例三:查询员工表中性别为男的数据

select eid,ename,gender from t_employee where gender='男';

二、别名查询

在查询某个字段或某张表时,我们可以给这个字段或这张表取一个别名,这种查询就叫别名查询。使用别名查询时,需要用到一个关键字叫 as ,但这个关键字在书写时是可以省略。

2.1、给字段名取别名

示例:查询员工表中姓名以及手机号,要求字段名取别名
select ename as '员工姓名',tel '手机号' from t_employee;

2.2、给表取别名

select eid, ename as name, gender, tel from t_employee as e;
为了验证表的别名已经起作用了,我们可以把别名加到字段的前面,作为字段的前缀。
select t_employee.eid, ename as name, gender, tel from t_employee as e;
上面的语句会报错,因为 t_employee 表已经被改为了 e 了,所以要修改为如下语句:
select e.eid, ename as name, gender, tel from t_employee as e;
在实际使用中,我们不推荐使用这种简短的别名,因为这种名称没有意义,最后是取一个见名知意的别名。

三、去重查询(distinct)

有时在查询时,我们希望查询出来的数据没有重复的数据,这时就可以使用 distinct 关键字来实 现。
示例:查询员工表中的性别数据
select gender from t_employee;

执行完上面的语句后,我们的需求是已经实现了,但是有问题,性别只需要男或女即可,不需要这些重复的数据。这时就需要去重查询。

select distinct gender from t_employee;

这个关键字好用,但是要慎用。因为它会做全表扫描,然后再把数据进行去重后再展示出来。比 较耗性能。

注意:这个关键字尽量少用或不用。

四、单表查询

4.1、算术运算符

算术运算符:+ - * /(除也可以写成div,div取整) %(取模可以写成mod)
示例一:筛选出eid是奇数的员工
select eid,ename,gender,tel from t_employee where eid%2=1;

示例二:筛选出eid除以2后等于1的数据

select eid,ename,gender,tel from t_employee where eid div 2=1;

4.2、比较运算符

比较运算符:= > >= < <= !=(不等于还可以写成<>) <=>(安全等于)
示例一:查询基本工资不等于 10000
select * from t_salary where basic_salary != 10000;

示例二: 查询 commission_pct 等于 null 数据

select * from t_salary where commission_pct = null;
从查询结果发现没有任何数据被查询出来,但是我们知道是有数据的值是 null。那为什么不能正常的查询出来呢?

原因在于做 null 查询时,不能使用 = 来匹配 null 值,而是要使用 is 来进行匹配。
(1)is:匹配等于
(2)is not:匹配不等于
select * from t_salary where commission_pct is null;

以后在对 null 进行查询时,不要使用等号(=)或不等号(!=),要使用 is 或 is not 来进 行筛选。

4.3、逻辑运算符

逻辑运算符包括与(&&)、或(||)、非(not)操作。
示例一:查询性别为男,并且在 1990 年1月2日以前出生的员工
select eid,ename,gender,birthday from t_employee where gender='男' and birthday < '1990-01-02';

上面的 SQL 语句也可以写成如下形式:

select eid,ename,gender,birthday from t_employee where gender='男' && birthday < '1990-01-02';

示例二:查询职位编号 job_id 是 1 或 2 的员工

select job_id, job_name, description from t_job where job_id=1 or job_id=2;

上面的SQL语句也可以写成如下形式:

select job_id, job_name, description from t_job where job_id=1 || job_id=2;

4.4、范围和集合

范围查询需要使用到 between...and... 来查询,而集合就需要使用 in 或 not in 来查询。
示例一:查询基本薪资在 9000~12000之间的员工编号和基本薪资。
select eid, basic_salary from t_salary where basic_salary between 9000 and 12000;

注意:between...and... 查询时,条件是一个闭区间查询。并且后一个值要大于前一个值。

上面的SQL语句和一面这条SQL语句执行结果是一样的。

select eid,basic_salary from t_salary where basic_salary>=9000 and basic_salary<=12000;

示例二:查询 eid 的值为 1、3、5的基本薪资。

select eid,basic_salary from t_salary where eid=1 or eid=3 or eid=5;

上面的SQL语句是可以正确的查询出我们想要的结果。但是这种查询效率很低,我们不推荐SQL 使用 or 查询,因为它会做全表扫描

一般来说,使用 or 查询的地方都可以使用 in 或 not in 来代替,这也是 SQL 优化中的一种方
式。
select eid,basic_salary from t_salary where eid in (1,3,5);
以上的SQL执行后,效果相同,但是它性能要比 or 查询高。
注意:使用 in 查询有局限性,它只支持数值类型和字符串类型。

4.5、模糊查询

模糊查询需要使用 like 关键字,也可使用 not like 来进行查询。要模糊查询中,我们还可以使用
通配符:
(1)%:表示 0 ~ n个字符
(2)_:表示一个字符
示例一:查询员工姓名中包含有 '孙' 的员工信息
select eid,ename,gender,tel from t_employee where ename like '%孙%';

上面SQL语句能够正常的查询出我们想要的数据,但是性能不好,它会进行全表扫描。我们可以查询以 '孙' 开头的员工。
select eid,ename,gender,tel from t_employee where ename like '孙%';
上面的SQL语句表示姓名是以 孙 开头的员工信息。
我们还可以使用下划线来进行匹配,一个下划线代表一个字符。
select eid,ename,gender,tel from t_employee where ename like '孙_';

上面的SQL语句表示姓名是以孙开头,并且名字中只有两个字。

4.6、统计查询

在 SQL 中,统计查询一般会用到如下几种:
(1)sum():用于求和统计
(2)avg():用于求平均值
(3)count():用于获取总记录数
(4)max():用于获取最大值
(5)min():用于获取最小值
示例一:查询员工的平均工资。
select avg(basic_salary) as '平均工资' from t_salary;

它的内部实际是把基本工资总和起来后再除以员工数得到值。
示例二:获取工资表中最高的工资。
select max(basic_salary) as '最高工资' from t_salary;

示例三:获取工资表中最低的工资

select min(basic_salary) as '最低工资' from t_salary;

示例四:获取所有员工的工资总和

select sum(basic_salary) as '工资总和' from t_salary;

示例五:获取员工总数

select count(*) as '员工总数' from t_salary;

使用 count() 来进行查询时,参数不能为空。参数可以是数字、字符、星号、某个字段,结果都一样。

4.7、分页查询(limit)

在MySQL查询中,要想实现分页查询,我们只需要提供 limit 关键字即可。这个关键字在使用时需要提供两个参数,语法格式如下:
limit offset,pagecount;
参数说明:

(1)offset:它是分页的起始偏移值,它需要计算;

(2)pagecount:显示的记录数

示例一:分页显示员工信息,每页显示 2 名员工。

第一页的数据
select eid,ename,gender,tel,birthday from t_employee limit 0, 2;

第二页的数据:
select eid,ename,gender,tel,birthday from t_employee limit 2, 2;

第三页数据:

select eid,ename,gender,tel,birthday from t_employee limit 4, 2;

从上面的三条SQL语句可以发现,唯一变化的是 offset 这个参数,而这个参数的计算是根据页码和每页显示的记录数来计算而得。它的计算公式为:

offset = (当前页码 - 1) * 每页显示记录数
offset = (page - 1) * pagecount

4.8、分组查询

分组查询需要使用 group by 关键字来进行分组,还可以能使用 having 关键字来进行条件筛选。

示例一:根据员工的职位来分组查询员工信息。

select eid,ename,gender,job_id from t_employee group by job_id;
执行上面的语句时,报了如下错误:
[42000][1055] Expression #1 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'mydb.t_employee.eid' which is not
functionally dependent on columns in GROUP BY clause; this is incompatible
with sql_mode=only_full_group_by
解决以上的错误的方式是把 select 中查询的字段放到 group by 的字段列表中即可。

select eid,ename,gender,job_id from t_employee group by eid,ename,gender,job_id;
这时再次查询时就不会报错误信息,并且能够查询出数据。

上面查询出来的数据并不是我们希望得到数据,我们是希望根据员工的职位来进行分组。
select job_id from t_employee group by job_id;

这次的结果确实是按员工的职位来进行分组,但是我们看不到每职位的员工有多少个。

从上面分析过程可以发现:我们的分组字段是可以写在 select 列表中的,还有统计查询的 一些函数也是可以写在分组查询中的 select 列表中的,其它的字段不能写在 select 列表 中。

示例二:根据部门编号统计员工人数
select dept_id, count(*) from t_employee group by dept_id;

示例三:根据部门编号统计员工人数,要求统计的部门编号大于1

select dept_id, count(*) from t_employee group by dept_id having dept_id > 1;
注意:当 SQL 语句中既有 where 条件,又有 having 条件时,先执行 where 条再执行 having条件。

select dept_id, count(*)from t_employeewhere gender = '男'group by dept_idhaving dept_id in (1,2);

从 SQL 优化角度,我们最好是定义 where 条件来过滤大量的数据后,再使用 having 来进行过滤,这样的话性能就会高很多。

4.9、排序查询

排序查询会使用到 order by 关键字,还会使用到 asc 或 desc 关键字。
示例一:查询所有员工信息,并按员工的编号的升序进行显示
select eid,ename,gender,tel,birthday from t_employee;
select eid,ename,gender,tel,birthday
from t_employee
order by eid desc ;

题目的要求是按员工的编号进行升序显示,因此在 SQL 就应该使用 order by 语句。

select eid,ename,gender,tel,birthday from t_employee
order by eid asc;

从上面两条 SQL 语句执行的结果可以发现,结果是一样的,也就是说,我们在查询时,默认就是按照主键的升序进行显示的。

如果是按主键的降序进行显示,那么就必须要使用 order by 语句了,并且指定为 desc。
select eid,ename,gender,tel,birthday
from t_employee
order by eid desc ;

示例二:按员工的基本工资的高低来进行显示

select eid, basic_salary
from t_salary
order by basic_salary desc;

对于排序的字段也是可以有多个的,当有多个时,会优先对第一个进行排序,只有当第一个的值相同时,才会对第二个值进行排序,依次类推。

示例三:按员工的职位高低进行排序,如果职位相同再按员工的编号升序进行排序。

select eid,ename,gender,job_id
from t_employee
order by job_id
desc,eid asc;

五、多表查询

5.1、笛卡尔积

什么是笛卡尔积,它其实是数学中的一个概念。当有多张表进行组合查询时,如果条件不准确,那么查询的结果就是这几张表中数据的乘积数。这种方式就是笛卡尔积。
笛卡尔积示例:查询员工姓名和所在部门名称
select ename,dname from t_employee,t_department;

从上面的查询结果可以发现,我们在做关联查询时, 要尽量避免出现笛卡尔积的情况出现。

5.2、关联查询

表的连接查询的约束条件有三种方式:

(1)where:适用于所有的关联查询。

(2)on:只能和 join一起使用,只能写在关联条件中,但是它可以和 where 一起使用。

(3)using:只能和 join 一起使用,并且要求两个关联的字段在关联表中名称一致

示例一:把关联条件写在 where 语句中

select ename,dname
from t_employee,t_department
where dept_id=did;

示例二:把关联条件写在 on 语句中

select ename,dname
from t_employee join t_department on dept_id=did;

两条查询的结果一样。

示例三:查询员工姓名和它的基本工资信息

select ename,basic_salary
from t_employee as e,t_salary as s
where e.eid=s.eid;

select ename,basic_salary
from t_employee e join t_salary ts on e.eid = ts.eid;

select ename,basic_salary
from t_employee join t_salary using (eid);

注意:要想使用 using,那么必须要关联查询相关表中被关联的条件字段名称必须相同。

示例四:查询员工姓名、基本工资和所在部门名称

这个示例涉及到三张表的关联查询:

(1)员工姓名:在 t_employee 表中

(2)基本工资:在 t_salary 表中

(3)部门名称:在 t_deparment 表中

员工表和基本工资表的关联条件是 eid。
员工表和部门表的关系条件是员工表中的 dept_id 字段指向的是部门表中的 did 字段。

工资表和部门表没有关联条件。
select ename,basic_salary,dname
from t_employee e join t_salary using(eid) join t_department td on td.did = e.dept_id;

5.3、内连接

在 MySQL 中内连接(inner join)有两种:显示的内连接和隐式的内连接,返回的结果是符合条件的所有数据。
隐式的内连接语法为:
select [columns]
from 表1,表2,...
where [condition];
显式的内连接语法为:
select [columns] from 表1 inner join 表2 on [condition] where [其它条件]; select [columns] from 表1 cross join 表2 on [condition] where [其它条件];select [columns] from 表1 join 表2 on [condition] where [其它条件];

执行关联查询后的结果为如上图所示的红色区域内的数据。也就是两张表中共同的部分。

示例一:查询员工姓名和部门名称
select ename,dname
from t_employee e inner join t_department td on td.did = e.dept_id;

select ename,dname
from t_employee e join t_department td on td.did = e.dept_id;
在使用 inner join 时,我们可以把 inner 关键字省略,只写 join 即可。

select ename,dname
from t_employee e cross join t_department td on td.did = e.dept_id;

5.4、外连接

外连接(outer join)在MySQL有以下几种外连接:
(1)左外连接(left outer join):简称左连接(left join)
(2)右外连接(right outer join):简称右连接(right join)
(3)全外连接(full outer join):简称全外连接(full join)

5.4.1、左外连接

左外连接是以左表要基准表,如果右表中没有数据和左表匹配,则以空值(null 值)为填充。
为了演示上面的效果,我们去添加一个新的部门。
insert into t_department(dname, description) values('财务部', '发工资部门');

示例一:查询所有部门信息以及部门员工信息

select did,dname,description, ename,gender,tel
from t_department td left join t_employee te on td.did=te.dept_id;

如果以员工表为左表,那么查询的结果就不一样:

select ename,gender,tel,did,dname,description
from t_employee te left join t_department td on te.dept_id=td.did;

上面的示例是返回左表中所有行,如果左表中的行在右表中没有匹配的数据,则会以空值来填充。

接下来我们来演示左表中行在右表中没有匹配的的记录。
示例二:查询部门信息,仅保留没有员工的部门信息
select did,dname,description,eid,ename,tel,dept_id
from t_department td left outer join t_employee te on td.did = te.dept_id
where te.dept_id is null;

示例三:查询所有员工信息,以及员工所在的部门信息

为了演示这个示例,我们去添加一名新员工。
insert into t_employee(ename) values('刘备');

5.4.2、右外连接

(1)返回右表中所有行,如果右表中的行在左表中没有匹配的行,则左表中以空值来填充。
示例一:查询所有部门以及该部门的员工信息。
select eid,ename,gender,did,dname,description
from t_employee e right join t_department d on e.dept_id=d.did;

(2)查询右表在左表中没有的数据。

示例二:查询没有员工的部门信息。

select eid,ename,gender,did,dname,description
from t_employee e right join t_department d on e.dept_id=d.did
where dept_id is null;

5.4.3、全外连接(union)

在 MySQL 中不支持全外连接(full join),但是我们可以使用 union 关键字来代替,它代表联合查询的意思。
可以使用如下语法来代替:

select column_list
from 表1 left join 表2
on 条件
union
select column_list
from 表1 right join 表2
on 条件
把左外连接和右外连接联合起来就形成了全外连接了。
(1)查询所有部门和员工信息

select did,dname,eid,ename,gender,tel
from t_department d left join t_employee te on d.did = te.dept_id
union
select did,dname,eid,ename,gender,tel
from t_department d right join t_employee te on d.did = te.dept_id;

(2)查询所有没有员工的部门和没有部门的员工信息

select did,dname,eid,ename,gender,tel
from t_department d left join t_employee te on d.did = te.dept_id
where te.dept_id is null
union
select did,dname,eid,ename,gender,tel
from t_department d right join t_employee te on d.did = te.dept_id
where te.dept_id is null;

5.5、自连接

当表1和表2是同一张表时,只是用了别名的方式来虚拟两张表进行关联查询时,就形成了自连接。在自连接中也是可以使用内连接和外连接的。
(1)查询员工姓名和它的领导的姓名,仅显示有领导的员工。
示例:修改员工的领导
update t_employee set mid=1 where eid=7;

此时孙红雷就是刘备的上级。
select e1.ename, e2.ename
from t_employee e1, t_employee e2
where e1.mid=e2.eid;

上面的方式我们使用隐式的内连接来实现的自连接查询。

还可以有如下的写法:
select e1.ename, e2.ename
from t_employee e1 join t_employee e2 on e1.mid=e2.eid;
上面的方式我们使用显式的内连接来实现的自连接查询。

六、子查询

在某些情况下,我们在做查询时,需要的条件或数据是另一个查询的结果,这时就用到子查询。
在 MySQL 中,子查询有以下几种:

(1)where 型子查询:把子查询作为 where 的条件(条件)

(2)from 型子查询:把子查询作为 from 的临时表(数据)

(3)exists 型子查询:把子查询作为判断是否存在的条件(条件)
(4)copy 型子查询:用于复制表数据或结构

6.1、where 型子查询

where 型子查询即把内层 sql 语句的查询结果作为外层 sql 的查询条件。
注意以下几点:
(1)子查询要包含在括号内
(2)建议将子查询放在比较条件的右侧
(3)单行操作符对应单行子查询,多行操作符就对应多行子查询
示例一:查询比孙红雷的工资高的员工信息
第一步:查询孙红雷的工资
select e.eid,ename,basic_salary
from t_employee e, t_salary s
where e.ename='孙红雷' and e.eid=s.eid;

第二步:查询工资比 12000 高的员工编号

select eid,basic_salary from t_salary
where basic_salary > 12000;

第三步:查询员工编号为 4 和 6 的员工信息

select eid,ename,gender,tel
from t_employee
where eid in (4,6);

根据上面三步的分析可以发现,我们所需要的条件都是另一个 SQL 语句的返回结果中,因此,我们可以使用子查询来实现上面三步的操作。

select eid,ename,gender,tel
from t_employee
where eid in (select eidfrom t_salarywhere basic_salary > (select basic_salaryfrom t_employee e, t_salary swhere e.ename='孙红雷' and e.eid=s.eid)
);
上面的SQL语句执行的结果是:

示例二:查询和孙红雷、鹿晗在同一个部门的员工
第一步:查询孙红雷和鹿晗所在的部门编号
select eid,ename, dept_id
from t_employee
where ename in ('孙红雷','鹿晗');

第二步:查询部门编号为 1 的员工信息
select eid,ename,gender,tel,dept_id
from t_employee
where dept_id in (1,1);

根据上面的分析,我们把第一步查询到的数据作为第二步查询所使用的条件即可。

select eid,ename,gender,tel,dept_id
from t_employee
where dept_id in (select dept_idfrom t_employeewhere ename in ('孙红雷','鹿晗')
);

执行结果一样

6.2、from 型子查询

from 型子查询是把内层 SQL 语句查询的结果作为外层 SQL 查询的数据。
示例:查询出比部门平均工资高的员工编号和基本工资。
第一步:查询出部门的平均工资
select dept_id, avg(basic_salary)
from t_employee e, t_salary s
where e.eid=s.eid
group by dept_id;

通过上面的SQL语句,我们可以正确得到部门的平均工资。但是有一个小的问题:我们从查询的结果可以看到,平均工资的字段名称为 avg(basic_salary) 。这个名称不便于我们使用,为了便于后续使用,我们经这个字段取一个别名,例如叫 avg_salary。

select dept_id, avg(basic_salary) as avg_salary
from t_employee e, t_salary s
where e.eid=s.eid group by dept_id;

通过这样调整后,后续使用就比较方便了。

第二步:查询员工信息和基本工资。
select e1.eid, dept_id,basic_salary
from t_employee e1 join t_salary s1
on e1.eid=s1.eid
and dept_id in (1, 2);

第三步:查询比部门平均工资高的员工和基本工资(需要把前面两步进行结合)

select te.eid, te.ename, basic_salary
from t_salary ts join t_employee teon ts.eid=te.eid
join (select dept_id, avg(basic_salary) as avg_salary from t_employee e, t_salary s where e.eid=s.eid group by dept_id ) as temp
on te.dept_id=temp.dept_id
where ts.basic_salary > temp.avg_salary;

6.3、exists 型子查询

exists 子查询基本都是出现在 where 语句中,用于判断某些条件是否成立。
示例:查询必须有员工的部门信息
第一步:查询有员工的部门

select eid,ename,gender,dept_id
from t_employee
where dept_id is not null;

第二步:在第一步的基础上来实现最终的功能

(1)测试子查询中有数据
select * from t_department
where exists(select * from t_employee);

(2)测试子查询中没有数据

select * from t_department
where exists(select * from t_employee where eid > 10);

 根据上面的测试情况可以发现,我们子查询的结果不能为空,如果为空则外层查询就没有数据了;如果子查询不为空,则外层查询就会有数据。

最终的查询语句为:
select did,dname,description
from t_department
where exists( select eid,ename,gender,dept_id from t_employee where dept_id is not null
);

上面的结果中包含了没有员工的部门——财务部,这显然不符合我们的题要求,原因在于我们子查询中条件给的不对,因为没有我外部表进行关联,导致条件不符合。

select did,dname,description
from t_department td
where exists( select eid,ename,gender,dept_id from t_employee te where td.did=te.dept_id
);

此时就得到了最终的结果了。

6.4、复制表子查询

复制表子查询分为两种情况:一种是复制表;另一种是复制数据。

6.4.1、复制表结构

语法格式:
create table 表名 like 被复制的表名;
示例:复制 t_stu 表结构。
原 t_stu 表的结构如下: desc t_stu
 create table t_stu_new like t_stu; 
执行完上面语句后,再执行:
desc t_stu_new;

6.4.2、复制表结构和数据

语法格式:
create table 表名 as (select * from 被复制的表名);
示例:复制 t_stu 表结构和数据
create table t_stu_new_2 as (select * from t_stu);desc t_stu_new_2;

select * from t_stu_new_2;

注意:这种语法可以复制表结构和数据,但是复制出来的表中不包含原表中的约束索引

6.4.3、复制表结构+数据

这种方式是先使用 6.4.1 节的语法来复制表结构,然后再使用如下语法来添加数据。
语法格式:
insert into 复制后的表名 select * from 被复制的表名;
示例:复制 t_stu 表结构和数据

create table t_stu_new_3 like t_stu;

再向这个表添加数据

insert into t_stu_new_3 select * from t_stu; select * from t_stu_new_3;

6.4.4、跨数据库复制

语法格式为:

create table 表名 like 被复制数据库名.被复制的表名; create table 新数据库名.表名 like 被复制数据库名.被复制的表名;

6.4.5、复制表中部分字段和数据

语法格式为:

create table 表名 as (
select 要复制的字段列表
from 被复制的表名
where 条件
)
示例:复制 t_stu 表中 id,name 字段到 t_stu_new_4 表中
create table t_stu_new_4 as (
select id,name
from t_stu
where id=1
)
使用如下语句来查看表结构:
desc t_stu_new_4;

再查看数据

lect * from t_stu_new_4;

6.4.6、在创建表的同时定义表的字段

语法格式为:

create table 表名(字段名 类型(长度) [主键][自增][非空][默认值][注释]
) as (
select 被复制的字段 from 被复制的表名
)
示例:
create table t_stu_new_5(
id int(11) primary key auto_increment
) as (
select id,name from t_stu );desc t_stu_new_5;

MySQL(零基础)详解之DQL相关推荐

  1. 【C零基础详解】Part1:7-1 计算摄氏温度 (10分)【变量的输入和输出】

    7-1 计算摄氏温度 (10分) 给定一个华氏温度F,本题要求编写程序,计算对应的摄氏温度C.计算公式:C=5×(F−32)/9.题目保证输入与输出均在整型范围内. 输入格式: 输入在一行中给出一个华 ...

  2. MySQL数据库基础详解

    文章大纲 一.数据库简介 二.Mysql数据库简介 三.Mysql安装与服务启动(Windows版本) 四.Mysql图形化工具 五.Mysql存储引擎精讲 六.Mysql数据类型介绍 七.Mysql ...

  3. MySQL数据库基础详解(非原创)

    文章大纲 一.数据库简介 二.Mysql数据库简介 三.Mysql安装与服务启动(Windows版本) 四.Mysql图形化工具 五.Mysql存储引擎精讲 六.Mysql数据类型介绍 七.Mysql ...

  4. Python零基础详解--商品详情、关键词搜索api

    为了进行此平台API的调用,首先我们需要做下面几件事情. 1. 获取一个KEY. 2. 参考API文档里的接入方式和示例. 3.查看测试工具是否有需要的接口,响应实例的返回字段是否符合参数要求. 4. ...

  5. FWT(快速沃尔什变换)零基础详解qaq(ACM/OI)

    1.前言(废话) 记得一年半之前做SRM518 Nim的时候还不知道FWT,当时自己用分治完美的水过去了.然后昨天的牛客有一道题,是说nim博弈中有n堆石子,请问最多取出多少堆石子可以让先手必败.当时 ...

  6. 思科ei ccie认证体系最新内容下一代编址IPV6技术最全面的基础详解 从零到精通必读

    思科ei ccie认证体系最新内容下一代编址IPV6技术最全面的基础详解 从零到精通必读 IPv6(Internet Protocol Version 6,因特网协议版本6)是网络层协议的第二代标准协 ...

  7. MySQL字段类型详解

    MySQL字段类型详解 2009-01-05 09:25 来源:泡菜博客 0个评论 分享文章 A- A+ 百度权重查询 词库网 网站监控 服务器监控 SEO监控 Swift编程语言教程 MySQL支持 ...

  8. 高并发架构系列:Redis缓存和MySQL数据一致性方案详解

    需求起因 在高并发的业务场景下,数据库大多数情况都是用户并发访问最薄弱的环节.所以,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问MySQL等数据库. 这个业务场景,主要 ...

  9. MySQL主从同步详解与配置

    https://zhuanlan.zhihu.com/p/335142300 MySQL主从同步详解与配置 第一部分[原理解析] * 应用背景* MySQL数据库自身提供的主从复制功能可以方便的实现数 ...

  10. 《MySQL安装流程详解》及《MySQL安装一直失败,重新安装显示已安装》

    <MySQL安装流程详解>及<MySQL安装一直失败,重新安装显示已安装> 本文由博主经过查阅网上资料整理总结后编写,如存在错误或不恰当之处请留言以便更正,内容仅供大家参考学习 ...

最新文章

  1. oracle安装清单过不去,oracle 11g(二)安装过程
  2. Android 开发常用代码片段
  3. java题目不会做那么解答_有几道JAVA的题目不会做 哪位高手来解答一下!谢
  4. 对象集合中如何用对象的某个属性给对象排序?
  5. 深入理解PHP之数组(遍历顺序)
  6. 进程和线程不属于标准c语言,经典C语言面试题6:进程与线程的关系和区别
  7. Farthest points Sampling on 3D meshes with mesh kept based on diffusion distance
  8. ubuntu目录结构
  9. 谷歌linux浏览器下载文件夹在哪,如何查看谷歌浏览器下载的文件路径?
  10. C语言中期报告格式,本科论文中期报告范文_本科毕业论文中期报告模板(2)
  11. 支持断点续传的大文件传输协议
  12. 论文阅读--SAP-SSE: Protecting Search Patterns and Access Patterns in Searchable Symmetric Encryption
  13. 我叫mt4公会攻城战服务器维护中,我叫MT4工会攻城战攻略玩法详解[多图]
  14. 台式电脑组装机相关知识之主板篇
  15. Hangfire详解
  16. C:素数(质数)的判断以及输出
  17. Android Q访问公共外部存储受限
  18. python中取对数怎么表示_python中取对数
  19. Java+SSM酒店管理系统旅店管理(含源码+论文+答辩PPT等)
  20. 【WordPress】修改固定链接,文章链接

热门文章

  1. 关闭cidaemon进程的方法
  2. Web实时语音/视频聊天/文件传输
  3. 尚硅谷kylin单机版之安装kylin
  4. [经验]iOS开发-记录下在开发过程中遇到的问题的解决方案及经验总结-1
  5. 压力单位PSI,PSIG, PSIA的区别
  6. 根据地址查询经纬度Js
  7. DSP编程时 c_int00是什么内容
  8. python自动上传图片_Python+selenium自动上传博客图片至新浪微博相册
  9. php theexcerpt,WordPress:使用the_excerpt函数显示摘要信息
  10. 【Python表白小程序】七夕表白神器(赶紧收藏起来)