准备工作:
注意:首先导入数据库,执行该语句前,必须先在Mysql服务器中创建company数据库,必须为空数据库。
C:\Users\adm>mysql -u root -p company < C:/backup/company.sql


1、复制表结构及数据到新表
CREATE TABLE 新表 SELECT * FROM 旧表
这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来删除。
不过这种方法的一个最不好的地方就是新表中没有了旧表的primary key、Extra(auto_increment)等属性。需要自己用"alter"添加,而且容易搞错。 


2、只复制表结构到新表
CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2
或CREATE TABLE 新表 LIKE 旧表 


3、复制旧表的数据到新表(假设两个表结构一样)
INSERT INTO 新表 SELECT * FROM 旧表 


4、复制旧表的数据到新表(假设两个表结构不一样)
INSERT INTO 新表(字段1,字段2,.......) SELECT 字段1,字段2,...... FROM 旧表


5、show create table 旧表;
这样会将旧表的创建命令列出。我们只需要将该命令拷贝出来,更改table的名字,就可以建立一个完全一样的表

一、简单数据记录查询
1、简单数据查询
# 查询所有字段数据
> select * from t_employee;
或者> select empno,ename,job,mgr,hiredate,sal,comn,deptno from t_employee;
# 查询指定字段数据
> select empno,ename from t_employee;

2、避免重复数据查询-distinct
> select job from t_employee;
> select distinct job from t_employee;

3、实现四则运算数据查询
> select ename,sal*12 from t_employee;
> select ename,sal*12 as yearsal from t_employee;其中 as可以省略。

4、设置显示格式数据查询
#利用函数concat()来连接字符串
> select concat(name,'的年薪为',sal*12) from t_employee;
> select concat(ename,'的年薪为',sal*12) yearsalary from t_employee;

二、条件数据记录查询
1、带关系运算符和逻辑运算符的条件数据查询
#单条件数据查询
>select ename from t_employee where job=’clerk’;
#多条件数据查询
> Select ename from t_employee where job='clerk'&& sal>800; 其中&&可以用and替换

2、带between and关键字的范围查询
> select ename from t_employee where sal between 1000 and 2000;

3、带is null关键字的空值查询
#空值记录的查询
> select ename from t_employee where comn is null;
#不是空值记录的查询
> select ename from t_employee where comn is not null;

4、带in关键字的集合查询
#在集合中的数据记录查询
> select ename from t_employee where empno in (7521,7782);
#不在集合中的数据记录查询
> select ename from t_employee where empno not in (7521,7782);
#集合中的数据记录查询可以包含null
> select ename from t_employee where empno in (7521,7782,null);
> select ename from t_employee where empno not in (7521,7782,null);
说明:查询的集合中如果存在null,则不会影响查询;如果使用关键字not in,查询的集合中如果存在null,则不会有任何的查询结果。

5、带like关键字的模糊查询
#带‘%’通配符的查询
> select ename from t_employee where ename like 'a%';
> select ename from t_employee where ename not like 'a%';
或者> select ename from t_employee where not ename like 'a%';
#带‘_’通配符的查询
> select ename from t_employee where ename like '_a%';
> select ename from t_employee where ename not like '_a%';
#带like关键字的模糊查询的注意点
> select ename from t_employee where ename not like '%a%';
> select ename from t_employee where ename  like '%a%';

三、排序数据记录查询
1、按照单字段排序
> select * from t_employee order by sal asc;
或者> select * from t_employee order by sal;
> select * from t_employee order by sal desc;

2、按照多字段排序
> select * from t_employee order by sal asc, hiredate desc;

四、限制数据记录查询数量
1、不指定初始位置
#显示记录数小于查询结果
> select * from t_employee where comn is null;
> select * from t_employee where comn is null limit 2;

#显示记录数大于查询结果
> select * from t_employee where comn is null limit 11;
注:查询有多少条comn是空的记录
> select count(empno) from t_employee where comn is null;

2、指定初始位置
> select * from t_employee where comn is null
> order by hiredate limit 0,5; 或者把0去掉,因为默认值从0开始
> select * from t_employee where comn is null
> order by hiredate limit 5,5;

五、统计函数和分组数据记录查询

1、统计函数
#统计数据记录条数
> select count(*) number from t_employee;
> select count(comn) number from t_employee;
#统计计算平均值
> select avg(comn) average from t_employee;
> select avg(comn) average from t_employee where not comn=0;
#统计计算求和
> select sum(sal) sumvalue from t_employee;
#统计计算最大值和最小值
> select max(sal) maxval, min(sal) minval from t_employee;

2、关于统计函数注意点

#首先构建一个只有结构没有数据的表t_employee
> create table t_employee3 select * from t_employee where 1=2;
#执行count()函数
> select count(deptno) number from t_employee3;
#同时执行多条统计函数
> select avg(deptno) average, sum(deptno) summer from t_employee3;

3、分组数据查询--简单分组查询
> select * from t_employee group by deptno;(每一条数据是随机产生的)

4、分组数据查询-实现统计功能分组查询
# group by 一般配合其他统计函数一起使用,比如
> select max(sal) maximalsal from t_employee group by deptno;
# 执行group_concat()语句
> select deptno, group_concat(ename) enames from t_employee group by deptno;

5、分组数据查询-实现多个字段分组查询
#按deptno分组
> select deptno from t_employee group by deptno;
#先按deptno分组,再按hiredate分组
> select deptno,hiredate from t_employee group by deptno,hiredate;
#执行group_concat()和count()
> select deptno,hiredate, group_concat(ename) enames
> from t_employee group by deptno, hiredate;
6、分组数据查询-having
#执行avg()
> select deptno, avg(sal) average
> from t_employee group by deptno;
#显示avg大于2000的分组
> select deptno, avg(sal) average
> from t_employee group by deptno
> having avg(sal)>2000;