一、复制表
- 格式
create table 表名 select查询语句
- 注意:复制成新表时,键值(pri,index等等)索引不会同步复制
- 案例
mysql> create table t2 select name,sex,age from user;
二、多表查询
2.1、 多表查询–>连接查询
-
将2个或2个以上的表按某个条件连接起来,从中选取需要的数据,表中包含某种相同意义的字段
-
交叉连接(交叉查询)如果查询不加where条件,则结果被称为笛卡尔集
-
自然连接(自己连自己)没啥意义
-
内连接:
-
外连接(使用的比较多,查表中重复的数值),外连接又分为:左连接和右连接
-
内外连接详解
2.2、交叉查询
- 交叉查询格式1:
select 字段名列表 from 表a,表b;
- 交叉查询格式2:
select 字段名列表 from 表a,表b where 条件
- 不加where时查询结果叫笛卡尔积(查询结果总条目数=表a的记录数*表b的记录数)
2.3、外连接:左连接,右连接
- 左连接:当条件成立时,以左表为主显示查询结果(当条件成立,将左表记录显示出来)
- on 匹配条件必须加
格式:select 字段名列表 from 表1 left join 表2 on 条件; # 表1为左表
- 右连接:当条件成立时,已右表为主显示查询结果(条件成立时显示右表的信息,如果左表没有匹配则显示null)
格式:select 字段名列表 from 表1 right join 表2 on 条件;
2.4、内连接
- 隐式内连接:select * from a,b where 条件;
- 显示内连接:select * from a inner join b on 条件;
- 与外连接类似(输出不同表的重复值)
2.5、where嵌套查询–>又叫where子查询
- 把内层的查询结果作为外层的查询条件,类似:ps aux |grep http
- 可以是同库同表,可以是不同库不同表
- 格式
select 字段名列表 from 表 where 条件(select 内层查询)
- 案例:查询表格中薪资大于平均薪资的人姓名
mysql> select name,pay from user where pay > (select avg(pay) from user);
2.6、案例:
- 多表查询案例-交叉查询1(笛卡尔集)
mysql> desc t5;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | NO | | NULL | |
| uid | int(2) | YES | | NULL | |
| shell | char(50) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc t6;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| name | char(20) | NO | | NULL | |
| uid | int(2) | YES | | NULL | |
| gid | int(2) | YES | | NULL | |
| homedir | char(50) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from t5,t6;
+------+------+---------------+------+------+------+----------------+
| name | uid | shell | name | uid | gid | homedir |
+------+------+---------------+------+------+------+----------------+
| bin | 1 | /sbin/nologin | bin | 1 | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin | bin | 1 | 1 | /sbin/nologin |
| bin | 1 | /sbin/nologin | adm | 3 | 4 | /var/adm |
| adm | 3 | /sbin/nologin | adm | 3 | 4 | /var/adm |
| bin | 1 | /sbin/nologin | lp | 4 | 7 | /var/spool/lpd |
| adm | 3 | /sbin/nologin | lp | 4 | 7 | /var/spool/lpd |
| bin | 1 | /sbin/nologin | sync | 5 | 0 | /sbin/nologin |
| adm | 3 | /sbin/nologin | sync | 5 | 0 | /sbin/nologin |
+------+------+---------------+------+------+------+----------------+
8 rows in set (0.00 sec)
- 交叉查询2
mysql> select t6.*,t5.shell from t5,t6 where t5.uid=t6.uid;
+------+------+------+---------------+---------------+
| name | uid | gid | homedir | shell |
+------+------+------+---------------+---------------+
| bin | 1 | 1 | /sbin/nologin | /sbin/nologin |
| adm | 3 | 4 | /var/adm | /sbin/nologin |
+------+------+------+---------------+---------------+
2 rows in set (0.00 sec)
- 外连接查询案例1:两表中显示显示重复的值
mysql> select * from t7;
+----------+------+----------------+
| name | uid | shell |
+----------+------+----------------+
| bin | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin |
| lp | 4 | /sbin/nologin |
| sync | 5 | /bin/sync |
| shutdown | 6 | /sbin/shutdown |
+----------+------+----------------+
5 rows in set (0.00 sec)
mysql> select * from t8;
+----------+------+----------------+
| name | uid | shell |
+----------+------+----------------+
| bin | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin |
| lp | 4 | /sbin/nologin |
| sync | 5 | /bin/sync |
| shutdown | 6 | /sbin/shutdown |
| halt | 7 | /sbin/halt |
| mail | 8 | /sbin/nologin |
| operator | 11 | /sbin/nologin |
+----------+------+----------------+
8 rows in set (0.00 sec)
- 以左连接查询显示两表中重复的数值1
mysql> select * from t7 left join t8 on t7.uid=t8.uid;
+----------+------+----------------+----------+------+----------------+
| name | uid | shell | name | uid | shell |
+----------+------+----------------+----------+------+----------------+
| bin | 1 | /sbin/nologin | bin | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin | adm | 3 | /sbin/nologin |
| lp | 4 | /sbin/nologin | lp | 4 | /sbin/nologin |
| sync | 5 | /bin/sync | sync | 5 | /bin/sync |
| shutdown | 6 | /sbin/shutdown | shutdown | 6 | /sbin/shutdown |
+----------+------+----------------+----------+------+----------------+
- 以左连接查询输出两表中相同的数值2(以表值少的为主表)
mysql> select t7.* from t7 left join t8 on t7.uid=t8.uid;
+----------+------+----------------+
| name | uid | shell |
+----------+------+----------------+
| bin | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin |
| lp | 4 | /sbin/nologin |
| sync | 5 | /bin/sync |
| shutdown | 6 | /sbin/shutdown |
+----------+------+----------------+
5 rows in set (0.00 sec)
- 以左连接查询输出两表中重复的值与不重复的值;(表值多的为主表)
mysql> select * from t8 left join t7 on t8.uid=t7.uid;
+----------+------+----------------+----------+------+----------------+
| name | uid | shell | name | uid | shell |
+----------+------+----------------+----------+------+----------------+
| bin | 1 | /sbin/nologin | bin | 1 | /sbin/nologin |
| adm | 3 | /sbin/nologin | adm | 3 | /sbin/nologin |
| lp | 4 | /sbin/nologin | lp | 4 | /sbin/nologin |
| sync | 5 | /bin/sync | sync | 5 | /bin/sync |
| shutdown | 6 | /sbin/shutdown | shutdown | 6 | /sbin/shutdown |
| halt | 7 | /sbin/halt | NULL | NULL | NULL |
| mail | 8 | /sbin/nologin | NULL | NULL | NULL |
| operator | 11 | /sbin/nologin | NULL | NULL | NULL |
+----------+------+----------------+----------+------+----------------+
三、MySQL图形化管理工具,phpmyadmin使用教程
基本没人使用,就不记录了