MYSQL基本命令

时间:2019-04-15
本文章向大家介绍MYSQL基本命令,主要包括MYSQL基本命令使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.mysql命令行不能用
    1.添加环境变量,分号连接,添加E:\phpStudy\PHPTutorial\MySQL\bin 到末尾,重启cmd

    2.cd E:\phpStudy\PHPTutorial\MySQL\bin


mysql基本命令

1.关闭mysql

net stop mysql

 

2.开启mysql

net start mysql


mysql -h IP -P 3306 -uroot -p123123

 

数据定义语言,定义了不同的数据段\数据库\表\列\索引等数据库对象.
常用语句包括create\drop\alter等.
1.创建数据库

CREATE DATABASE dbname

例子:create database test1;

2.查看数据库

show databases


3.使用数据库

USE dbname


4.删除数据库

drop database dbname


5.创建表


mysql> create table user_p(
    -> id int not null primary key auto_increment,
    -> name char(20),
    -> age tinyint(3),
    -> banji varchar(10),
    -> chengji int,
    -> sex tinyint(3)
    -> )engine=innodb auto_increment=1 default charset=utf8;
Query OK, 0 rows affected (0.08 sec)


6.查看表

1>desc tbname;
mysql> desc user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(20)    | YES  |     | NULL    |                |
| age     | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)
2>show create table tbname \G; 更详细表信息
mysql> show create table user_p \G
*************************** 1. row ***************************
       Table: user_p
Create Table: CREATE TABLE `user_p` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) DEFAULT NULL,
  `age` tinyint(3) DEFAULT NULL,
  `banji` varchar(10) DEFAULT NULL,
  `chengji` int(11) DEFAULT NULL,
  `sex` tinyint(3) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


7.删除表

drop table tbname;


8.修改表

 
1>修改表字段类型

alter table tbname modify[colunm] colunm_definition[first|after col_name]

例如:

修改表test的ename字段定义,将varchar(10)改为varchar(20)

alter table test modify ename varchar(20);
###############
mysql> desc user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(20)    | YES  |     | NULL    |                |
| age     | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

mysql> alter table user_p modify name char(36);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(36)    | YES  |     | NULL    |                |
| age     | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

 


2>字段改名
alter table tbname change[colunm] old_col_name column_definition [first|after col_name]
将age改名为age1,同时修改字段类型为int(4)
alter table test change age age1 int(4);
alter table test change age age int(4); //只修改属性
###############
mysql> alter table user_p change age age1 tinyint(3);
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc  user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(36)    | YES  |     | NULL    |                |
| age1    | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

 

 


3>增加表字段

alter table tbname add[column] column_definition [first|after col_name]
例如:
alter table test add column age int(3);
###############
mysql> alter table user_p add column phone char(15);
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc  user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(36)    | YES  |     | NULL    |                |
| age1    | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
| phone   | char(15)    | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

 


4>删除表字段
alter table tbname drop[column] col_name;
例如:
alter table test drop column age;
#########################
mysql> alter table user_p drop column phone;
Query OK, 0 rows affected (0.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc  user_p;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | char(36)    | YES  |     | NULL    |                |
| age1    | tinyint(3)  | YES  |     | NULL    |                |
| banji   | varchar(10) | YES  |     | NULL    |                |
| chengji | int(11)     | YES  |     | NULL    |                |
| sex     | tinyint(3)  | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)


6.修改表名字

alter table tbname rename[to] new_tbname
例子:
alter table test rename test2;
#########################
mysql> alter table user_p rename user_j;
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| cross_a        |
| dian_b         |
| dian_sex       |
| jiangli        |
| sex_a          |
| sex_b          |
| user_a         |
| user_b         |
| user_j         |====user_p
| user_t         |
| userpk         |
+----------------+
11 rows in set (0.00 sec)

 


DML(data manipulation language)语句:

数据操纵语句,用于添加\删除\更新和查询数据库记录,并检查数据完整性.

常用语句insert\delete\update和select.

1.插入记录

1>插入一条

insert into tbname(field1,field2…) values(val1,val2…);

例子:

inset into test(ename,info) values(‘小白‘,2);
###################################################
mysql> insert into user_j(name,age1,banji,chengji,sex) values('xiaobai',20,'php
801',500,1);
Query OK, 1 row affected (0.05 sec)

mysql> select * from user_j;
+----+---------+------+---------+---------+------+
| id | name    | age1 | banji   | chengji | sex  |
+----+---------+------+---------+---------+------+
|  1 | xiaobai |   20 | php1801 |     500 |    1 |
+----+---------+------+---------+---------+------+
1 row in set (0.00 sec)

 


2>插入多条

insert into tbname(field1,field2…)
values
(val1,val2…),(val1,val2…),……(val1,val2…);
##############################################
mysql> insert into user_j(name,age1,banji,chengji,sex) values('dongjinhua',15,'p
hp1803',800,1),('chengguanghao',38,'php1803',50,1);
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from user_j;
+----+---------------+------+---------+---------+------+
| id | name          | age1 | banji   | chengji | sex  |
+----+---------------+------+---------+---------+------+
|  1 | xiaobai       |   20 | php1801 |     500 |    1 |
|  2 | dongjinhua    |   15 | php1803 |     800 |    1 |
|  3 | chengguanghao |   38 | php1803 |      50 |    1 |
+----+---------------+------+---------+---------+------+
3 rows in set (0.00 sec)

 

成倍赋值:自己插自己,测试数据用
##########################
mysql> insert into dian_b(name,sex_id) select name,sex_id from dian_b;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select count(1) from dian_b;
+----------+
| count(1) |
+----------+
|        8 |
+----------+
1 row in set (0.00 sec)

mysql> insert into dian_b(name,sex_id) select name,sex_id from dian_b;
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

 

 

 


2.更新记录

1>更新一个表中的数据

update tbname set field1=value1,field2=val2,… [where condition]

例子:

将表test中的ename为“lisa”的薪资(sal) 从2000改为4000

update test set sal=4000 where ename=’lisa’;
######################################################
mysql> update user_j set banji='php1804' where name='dongjinhua';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from user_j;
+----+---------------+------+---------+---------+------+
| id | name          | age1 | banji   | chengji | sex  |
+----+---------------+------+---------+---------+------+
|  1 | xiaobai       |   20 | php1801 |     500 |    1 |
|  2 | dongjinhua    |   15 | php1804 |     800 |    1 |
|  3 | chengguanghao |   38 | php1803 |      50 |    1 |
|  4 | dongjinhua    |   15 | php1804 |     800 |    1 |
|  5 | chengguanghao |   38 | php1803 |      50 |    1 |
+----+---------------+------+---------+---------+------+
5 rows in set (0.00 sec)

 

 

 

3.删除记录

1>删除一个表中的记录

delete from tbname [where condition]

例子:

delete from test where ename=’xiaobai’;
#####################################
mysql> delete from user_j where id=1;
Query OK, 1 row affected (0.02 sec)

mysql> select * from user_j;
+----+---------------+------+---------+---------+------+
| id | name                  | age1| banji        | chengji | sex  |
+----+---------------+------+---------+---------+------+
|  2 | dongjinhua         |   15  | php1804 |     800   |    1 |
|  3 | chengguanghao |   38  | php1803 |      50    |    1 |
|  4 | dongjinhua         |   15  | php1804 |     800   |    1 |
|  5 | chengguanghao |   38  | php1803 |      50    |    1 |
+----+---------------+------+---------+---------+------+
4 rows in set (0.00 sec)

 

2>删除多个表的记录???

delete tb1,tb2..tbn from tb1,tb2..tbn [where condition]

例子:

同时删除表emp和dept中的deptno为3的记录

delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=3;

备注:

如果from后面的表名用别名,则delete后面也要用相应的别名,否则会提示语法错误.

 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

保证添加足够的测试数据


mysql基础–查询语句select使用

1.查询不重复记录
select distinct deptno from emp;

############################
mysql> select distinct name from user_j;
+---------------+
| name          |
+---------------+
| dongjinhua    |
| chengguanghao |
| quhaijing     |
| mahuan        |
| songjiafei    |
| zhangxinlei   |
| licheng       |
| qihaolong     |
| yizihao       |
+---------------+
9 rows in set (0.02 sec)

 

2.条件查询

select * from where deptno=1;

select * from where deptno=1 and sal<2000;

##########################################
mysql> select * from user_j where name='dongjinhua';
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
|   2 | dongjinhua |   15 | php1804 |     800 |    1 |
|   4 | dongjinhua |   15 | php1804 |     800 |    1 |
|  12 | dongjinhua |   15 | php1804 |     800 |    1 |
|  14 | dongjinhua |   15 | php1804 |     800 |    1 |
|  27 | dongjinhua |   15 | php1804 |     800 |    1 |
|  29 | dongjinhua |   15 | php1804 |     800 |    1 |
|  37 | dongjinhua |   15 | php1804 |     800 |    1 |
|  39 | dongjinhua |   15 | php1804 |     800 |    1 |
|  58 | dongjinhua |   15 | php1804 |     800 |    1 |
|  60 | dongjinhua |   15 | php1804 |     800 |    1 |
|  68 | dongjinhua |   15 | php1804 |     800 |    1 |
|  70 | dongjinhua |   15 | php1804 |     800 |    1 |
|  78 | dongjinhua |   15 | php1804 |     800 |    1 |
|  80 | dongjinhua |   15 | php1804 |     800 |    1 |
|  88 | dongjinhua |   15 | php1804 |     800 |    1 |
|  90 | dongjinhua |   15 | php1804 |     800 |    1 |
| 121 | dongjinhua |   15 | php1804 |     800 |    1 |
| 123 | dongjinhua |   15 | php1804 |     800 |    1 |
| 131 | dongjinhua |   15 | php1804 |     800 |    1 |
| 133 | dongjinhua |   15 | php1804 |     800 |    1 |
。。。。。。。
mysql> insert into user_j(name,age1,banji,chengji,sex) values('dongjinhua',25,'p
hp1805',50,2);
Query OK, 1 row affected (0.04 sec)
mysql> select * from user_j where name='dongjinhua' and banji='php1805';
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
| 504 | dongjinhua |   25 | php1805 |      50 |    2 |
+-----+------------+------+---------+---------+------+
1 row in set (0.00 sec)

 

 

 

 

3.排序和限制

1>常用取出按照某个字段进行排序后的记录结果,用关键字order by.

desc 降序,asc 升序,默认是升序

select * from emp order by sal;
##############################################
mysql> select * from user_j order by chengji limit 9;
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
| 254 | songjiafei |   68 | php1803 |      30 |    1 |
| 157 | songjiafei |   68 | php1803 |      30 |    1 |
| 334 | songjiafei |   68 | php1803 |      30 |    1 |
|  84 | songjiafei |   68 | php1803 |      30 |    1 |
| 274 | songjiafei |   68 | php1803 |      30 |    1 |
|  33 | songjiafei |   68 | php1803 |      30 |    1 |
| 324 | songjiafei |   68 | php1803 |      30 |    1 |
| 167 | songjiafei |   68 | php1803 |      30 |    1 |
| 314 | songjiafei |   68 | php1803 |      30 |    1 |
+-----+------------+------+---------+---------+------+
9 rows in set (0.00 sec)

select * from emp order by sal desc;
###################################
mysql> select distinct name,chengji  from user_j order by chengji desc limit 9;
+---------------+---------+
| name          | chengji |
+---------------+---------+
| licheng       |    2600 |
| dongjinhua    |     800 |
| mahuan        |     300 |
| yizihao       |     260 |
| zhangxinlei   |     260 |
| quhaijing     |     100 |
| qihaolong     |      80 |
| chengguanghao |      50 |
| dongjinhua    |      50 |
+---------------+---------+
9 rows in set (0.00 sec)

 

2>对于排序后相同的字段进行再次排序

select * from emp order by sal, deptno desc;
###############################
mysql> select * from user_j  where name='dongjinhua' order by name,banji;
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
| 400 | dongjinhua |   15 | php1804 |     800 |    1 |
| 318 | dongjinhua |   15 | php1804 |     800 |    1 |
| 310 | dongjinhua |   15 | php1804 |     800 |    1 |
| 308 | dongjinhua |   15 | php1804 |     800 |    1 |
| 300 | dongjinhua |   15 | php1804 |     800 |    1 |
。。。。。。。。
| 141 | dongjinhua |   15 | php1804 |     800 |    1 |
| 133 | dongjinhua |   15 | php1804 |     800 |    1 |
| 131 | dongjinhua |   15 | php1804 |     800 |    1 |
| 123 | dongjinhua |   15 | php1804 |     800 |    1 |
| 504 | dongjinhua |   25 | php1805 |      50 |    2 |
+-----+------------+------+---------+---------+------+
65 rows in set (0.00 sec)
mysql> select * from user_j  where name='dongjinhua' order by name,banji desc;
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
| 504 | dongjinhua |   25 | php1805 |      50 |    2 |
| 320 | dongjinhua |   15 | php1804 |     800 |    1 |
| 318 | dongjinhua |   15 | php1804 |     800 |    1 |
。。。。。。。。
| 131 | dongjinhua |   15 | php1804 |     800 |    1 |
| 123 | dongjinhua |   15 | php1804 |     800 |    1 |
|   2 | dongjinhua |   15 | php1804 |     800 |    1 |
+-----+------------+------+---------+---------+------+
65 rows in set (0.00 sec)
3>对于排序后的记录只显示一部分使用limit

select * from tbname……[limit 偏移量,记录行数]

例子:

select * from emp order by sal limit 3;

select * from emp order by sal limit 2,3;从第三条开始

备注:

默认起始偏移量为0,只写记录行数就行!!!! limit 0,3 等价 limit 3
######################
mysql> select * from user_j  where name='dongjinhua' order by name,banji desc limit 3;
+-----+------------+------+---------+---------+------+
| id  | name       | age1 | banji   | chengji | sex  |
+-----+------------+------+---------+---------+------+
| 504 | dongjinhua |   25 | php1805 |      50 |    2 |
| 320 | dongjinhua |   15 | php1804 |     800 |    1 |
| 318 | dongjinhua |   15 | php1804 |     800 |    1 |
+-----+------------+------+---------+---------+------+
3 rows in set (0.00 sec)

 


4.聚合

聚合一般用于数据统计

语法:
select [field1,field2..]fun_name
from tbname
[where where_conditon]
[group by field1,field2..[with rollup]]
[having where_condition]

参数说明:
fun_name 表示要做的聚合操作,也就是聚合函数,常用的有sum(求和)\count(*)(记录数)\max(最大值)\min(最小值).
group by 关键字表示和聚合的字段,比如按照部门分类统计员工数量,group by后就写部门
with rollup 表示对分类聚合后的结果进行再汇总,记录是上面所有记录的总和。
having 表示对分类后的结果进行条件过滤
备注:
having对聚合后的结果进行过滤,where是在聚合前先进行过滤
有必要的话可以先where后进行聚合操作可以提高效率


group by  对哪个字段进行分组 group by 后边跟哪个字段


1.查看表中所有的记录个数
select count(*) from user_j;
###################
mysql> select count(*) from user_j;
+----------+
| count(*) |
+----------+
|      322 |
+----------+
1 row in set (0.00 sec)


2.as别名 :当我字段名特别长不好处理,没法处理 或者链接查询时候需要更短的sql可以使用别名
######################
mysql> select count(*) as jilugeshu from user_j;
+-----------+
| jilugeshu |
+-----------+
|       322 |
+-----------+
1 row in set (0.00 sec)

 

3.统计每个班有多少人
#################################
mysql> select banji,count(*) from user_j  group by banji;
+---------+----------+
| banji   | count(*) |
+---------+----------+
| php1801 |       32 |
| php1802 |       64 |
| php1803 |      128 |
| php1804 |       96 |
| php1805 |        2 |
+---------+----------+
5 rows in set (0.00 sec)

 

4.既要统计各班人数,又要统计总人数
###########################
mysql> select banji,count(*) from user_j group by banji with rollup;
+---------+----------+
| banji   | count(*) |
+---------+----------+
| php1801 |       32 |
| php1802 |       64 |
| php1803 |      128 |
| php1804 |       96 |
| php1805 |        2 |
| NULL    |      322 |
+---------+----------+
6 rows in set (0.01 sec)

5.统计人数大于2的班级
#######################
mysql> select banji,count(*) from user_j group by banji having count(*)>2;
+---------+----------+
| banji   | count(*) |
+---------+----------+
| php1801 |       32 |
| php1802 |       64 |
| php1803 |      128 |
| php1804 |       96 |
+---------+----------+
4 rows in set (0.00 sec)


sum()求和
max()最大值
min()最小值
avg()平均值

6.统计学校所有同学的成绩总额\最高和最低
##############################
mysql> select sum(chengji),max(chengji),min(chengji) from user_j;
+--------------+--------------+--------------+
| sum(chengji) | max(chengji) | min(chengji) |
+--------------+--------------+--------------+
|       162550 |         2600 |           30 |
+--------------+--------------+--------------+
1 row in set (0.00 sec)

7.求出所有同学的平均成绩
#####################################
mysql> select avg(chengji) from user_j;
+--------------+
| avg(chengji) |
+--------------+
|     504.8137 |
+--------------+
1 row in set (0.00 sec)

8.求出每个班的最高成绩和最低成绩,以及班里的平均分
#################################
mysql> select banji,max(chengji),min(chengji),avg(chengji) from user_j group by banji;
+---------+--------------+--------------+--------------+
| banji   | max(chengji) | min(chengji) | avg(chengji) |
+---------+--------------+--------------+--------------+
| php1801 |          100 |          100 |     100.0000 |
| php1802 |         2600 |          300 |    1450.0000 |
| php1803 |           80 |           30 |      52.5000 |
| php1804 |          800 |          260 |     620.0000 |
| php1805 |          260 |           50 |     155.0000 |
+---------+--------------+--------------+--------------+
5 rows in set (0.00 sec)


9.求出班里人数大于30的班级 最高成绩;
#######################################
mysql> select banji,max(chengji) from user_j group by banji having count(*)>30;
+---------+--------------+
| banji   | max(chengji) |
+---------+--------------+
| php1801 |          100 |
| php1802 |         2600 |
| php1803 |           80 |
| php1804 |          800 |
+---------+--------------+
4 rows in set (0.00 sec)

10.得出班里同学最高成绩不小500的 班级
###################################
mysql> select banji from user_j group by banji having max(chengji)>500;
+---------+
| banji   |
+---------+
| php1802 |
| php1804 |
+---------+
2 rows in set (0.00 sec)


11.求出班里人数大于30的班级 最高成绩 排除1803班
having对聚合后的结果进行过滤,where是在聚合前先进行过滤
有必要的话可以先where后进行聚合操作可以提高效率
###################################
结合第 9 个来看
mysql> select banji,max(chengji) from user_j where banji!='php1803' group by banji having count(*)>
0;
+---------+--------------+
| banji   | max(chengji) |
+---------+--------------+
| php1801 |          100 |
| php1802 |         2600 |
| php1804 |          800 |
+---------+--------------+
3 rows in set (0.00 sec)

 


5.表连接

内连接: 只连接匹配的行
左外连接: 包含左边表的全部行(不管右边的表中是否存在与它们匹配的行),以及右边表中全部匹配的行
右外连接: 包含右边表的全部行(不管左边的表中是否存在与它们匹配的行),以及左边表中全部匹配的行


效率问题:
1.inner join比left join快
注:inner join  内连接等价于下面的sql: SELECT A.name, B.address FROM A, B WHERE A.id = B.A_id所以一般要用一般的连接就可以了.

###################################
内连接: 只连接匹配的行
mysql> select * from user_j,sex_b where user_j.sex=sex_b.sex_id limit 30;
+----+---------------+------+---------+---------+------+--------+-----------+
| id | name          | age1 | banji   | chengji | sex  | sex_id | sex_title |
+----+---------------+------+---------+---------+------+--------+-----------+
|  2 | dongjinhua    |   15 | php1804 |     800 |    1 |      1 | nan       |
|  3 | chengguanghao |   38 | php1803 |      50 |    1 |      1 | nan       |
|  4 | dongjinhua    |   15 | php1804 |     800 |    1 |      1 | nan       |
|  5 | chengguanghao |   38 | php1803 |      50 |    1 |      1 | nan       |
|  6 | quhaijing     |   28 | php1801 |     100 |    2 |      2 | nv        |
。。。

注:没有重复字段名的时候可以用*将两张表的所有字段显示出来,没意义,不建议

######################################
内连接: 只连接匹配的行
mysql> select name,banji,age1,chengji,sex_b.sex_title as sex_title from user_j,sex_b where user_j.sex=sex_b.sex_i
d limit 30;
+---------------+---------+------+---------+-----------+
| name          | banji   | age1 | chengji | sex_title |
+---------------+---------+------+---------+-----------+
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
| quhaijing     | php1801 |   28 |     100 | nv        |
| mahuan        | php1802 |   18 |     300 | nv        |
。。。。。。。。。。
注:尽量规范select 字段的 书写方式,在关联查询的时候将字段名标识上表名并且给予别名

 


:inner join  内连接等价于下面的sql: SELECT A.name, B.address FROM A, B WHERE A.id = B.A_id所以一般要用一般的连接就可以了.
######################################
内连接: 只连接匹配的行
mysql> select name,banji,age1,chengji,sex_b.sex_title from user_j inner join sex_b on user_j.sex=sex
_b.sex_id limit 30;
+---------------+---------+------+---------+-----------+
| name          | banji   | age1 | chengji | sex_title |
+---------------+---------+------+---------+-----------+
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
| quhaijing     | php1801 |   28 |     100 | nv        |
| mahuan        | php1802 |   18 |     300 | nv        |
| songjiafei    | php1803 |   68 |      30 | nan       |
| zhangxinlei   | php1804 |   18 |     260 | nan       |
| licheng       | php1802 |   58 |    2600 | nan       |
| qihaolong     | php1803 |   38 |      80 | nan       |
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
| dongjinhua    | php1804 |   15 |     800 | nan       |
| chengguanghao | php1803 |   38 |      50 | nan       |
........


######################################
2>左连接:包含所有左边表中的记录甚至是右边表中没有和它匹配的记录
mysql> select * from dian_b;
+----+----------+--------+
| id | name     | sex_id |
+----+----------+--------+
|  1 | xiaohong |      1 |
|  2 | xiaobai  |      1 |
|  3 | xiaohei  |      2 |
|  5 | xiaowu   |      5 |
............
| 30 | xiaohong |      1 |
| 31 | xiaobai  |      1 |
| 32 | xiaohei  |      2 |
| 33 | xiaowu   |      5 |
+----+----------+--------+
32 rows in set (0.00 sec)

mysql> select * from sex_b;
+--------+-----------+
| sex_id | sex_title |
+--------+-----------+
|      1 | nan       |
|      2 | nv        |
|      3 | renyao    |
|      4 | renshou   |
+--------+-----------+
4 rows in set (0.00 sec)

mysql> select name,sex_b.sex_title as sex from dian_b left join sex_b on dian_b.sex_id=sex_b.sex_id

+----------+------+
| name     | sex  |
+----------+------+
| xiaohong | nan  |
| xiaobai  | nan  |
| xiaohei  | nv   |
| xiaowu   | NULL |
.........
| xiaohong | nan  |
| xiaobai  | nan  |
| xiaohei  | nv   |
| xiaowu   | NULL |
+----------+------+
32 rows in set (0.00 sec)


&&&所有链接中(内,左,右)  on dian_b.sex_id=sex_b.sex_id <=> using(sex_id)
如果两张关联表匹配的两个字段名字相同可以使用using(sex_id)
##########################################
mysql> select name,sex_b.sex_title as sex from dian_b left join sex_b using(sex_id);
+----------+------+
| name     | sex  |
+----------+------+
| xiaohong | nan  |
| xiaobai  | nan  |
| xiaohei  | nv   |
| xiaowu   | NULL |
| xiaohong | nan  |
| xiaobai  | nan  |
| xiaohei  | nv   |
| xiaowu   | NULL |
| xiaohong | nan  |
| xiaobai  | nan  |
| xiaohei  | nv   |
。。。。。。。。

 


6.子查询

某些情况下需要查询的时候,需要的条件是另一个select语句的结果,这时候用子查询.

用于子查询的关键字包括(in|not in|=|!=|exists|not exists)等.
例子:
1>in 条件记录为多个
select * from emp where deptno in(select deptno from dept)
2.= 条件记录为1
select * from emp where deptno=(select deptno from dept where id=1);
备注:

某些情况下表连接可以替代子查询用于优化
############################################
mysql> select distinct user_id from jiangli;
+---------+
| user_id |
+---------+
|       1 |
|       2 |
+---------+
2 rows in set (0.00 sec)

mysql> select * from user_j where id in(1,2);
+----+------------+------+---------+---------+------+
| id | name       | age1 | banji   | chengji | sex  |
+----+------------+------+---------+---------+------+
|  2 | dongjinhua |   15 | php1804 |     800 |    1 |
+----+------------+------+---------+---------+------+
1 row in set (0.00 sec)

mysql> select * from user_j where id in(select distinct user_id from jiangli);
+----+------------+------+---------+---------+------+
| id | name       | age1 | banji   | chengji | sex  |
+----+------------+------+---------+---------+------+
|  2 | dongjinhua |   15 | php1804 |     800 |    1 |
+----+------------+------+---------+---------+------+
1 row in set (0.00 sec)

 

 

 

7.记录联合

有时候需要将两张表的记录查询出来之后合并到一起显示

select deptno from emp
union [all]
select deptno from dept;

union 合并到一起并且进行一次distinct去重
union all 合并到一起显示

user
user1(id:1-100) user2(id:101-200)  user3(id:201-300)   desc属性相同
#######################
mysql> select name from user_a union select name from user_b;
+-----------+
| name      |
+-----------+
| xiaowang  |
| xiaoli    |
| xiaozu    |
| xiaohuang |
| xiaogao   |
| xiaozhou  |
| xiaoyang  |
| xiaosong  |
| xiaozhang |
+-----------+
9 rows in set (0.05 sec)
mysql> select name from user_a union all select name from user_b limit 30;
+-----------+
| name      |
+-----------+
| xiaowang  |
| xiaoli    |
| xiaozu    |
| xiaohuang |
| xiaogao   |
| xiaozhou  |
| xiaoyang  |
| xiaosong  |
| xiaozhang |
| xiaowang  |
| xiaoli    |
| xiaozu    |
| xiaohuang |
| xiaogao   |
| xiaozhou  |
| xiaoyang  |
| xiaosong  |
.............................

 

 

 

 

 

 

 

 

 

 

上一页 下一页

原文地址:http://www.manongjc.com/article/103234.html