1.嵌套查询
定义:
把内层的查询结果作为外层查询的条件
语法:
sql查询语句 where 条件(sql查询语句);
示例1(例userinfo表操作):
1)把uid的值小于这个字段的平均值的用户名和uid显示出来
select username,uid from userinfo where uid<(select avg(uid) from userinfo);
2)查找userinfo表中username的值在mysql库下的user表中的host字段值为
localhost并且user字段的值是root的用户名
select username from userinfo where username in (select user
from mysql.user where host="localhost" and user="root");
示例2:
1).tb_user中最晚注册的用户(user_name,user_createdat)
select user_name,user_createdat late
from tb_user where user_createdat = (select max(user_createdat) from tb_user);
2).tb_user中每个城市最晚注册时间(user_city,user_createdat)
select user_city,max(user_createdat) late from tb_user group by user_city;
3).tb_user中每个城市最晚注册的用户(user_name,user_city,user_createdat)
select user_name,user_city,user_createdat from tb_user where user_createdat in (select
max(user_createdat) from tb_user group by user_city);
4).tb_user中最晚注册的城市的用户(user_name,user_city,user_createdat)
select user_name,user_city,user_createdat from tb_user where user_city in (select
user_city from tb_user where user_createdat in (select max(user_createdat) from tb_user));
2.多表查询
两种方式:
1).select 字段名列表 from 表名列表1,表名2; -->笛卡尔集
示例:
1>.select * from t1,t2;
select t1.username,t1.uid,t2.username from t1,t2;
2).select 字段名列表 from 表名列表 where 条件;
示例:
1>.找到t1表和t2表中相同的用户名,全部显示信息
select * from t1,t2 where t1.username=t2.username;
2>.找到t1和t2表中相同的uid号,把两张表的uid和username都显示出来
select t1.uid,t1.username,t2.uid,t2.username from t1,t2 where t1.uid=t2.uid;
3.连接查询
先创建两张表
create table t3 select username,uid from userinfo limit 2;
create table t4 select username,uid from userinfo limit 3;
1).左连接
select 字段名 from 表1 left join 表2 on 条件;(以左边的表1为主)
示例:
把t3和t4表中username相同的记录显示出来,以t3为主
select * from t3 left join t4 on t3.username=t4.username;
比较insert into t3 values("MYSQL",888),("Python",666);后再执行上句
2).右连接
select 字段名 from 表1 right join 表2 on 条件;(以右边的表2为主)
内连接 inner join在MySQL中用join也可代表inner join
外连接 左外联接left [outer] join
右外联接right [outer] join
create table t1(
customer_id varchar(10),
city varchar(10) not null,
primary key(customer_id)
)default charset = utf8;
create table t2(
order_id int auto_increment,
cid varchar(10),
primary key(order_id)
)default charset = utf8;
insert into t1 values('tedu','hz'),('jd','bj'),('tx','bj'),('bd','sh');
insert into t2(cid) values('tedu'),('jd'),('jd'),('tx'),(NULL);
t1和t2做无条件内连接,得到一个t1和t2所有可能的数据记录的组合
形成的表.大表又称为t1和t2的笛卡尔积
select * from t1 join t2
t1和t2表做有条件内连接,从t1和t2的笛卡尔积中
筛选出符合条件的那些数据记录组成一个数据表
select * from t1 join t2 on t1.customer_id = t2.cid
只有在经过有条件内连接后获得的数据表上,才能做左外联接或右外连接.
左外联接或右外连接就是在有条件内连接表基础上,必须将主表记录完整显示的数据表
select * from t1 left join t2 on t1.customer_id = t2.cid;
select * from t1 right join t2 on t1.customer_id = t2.cid;
select user_name,user_city,user_createdat,
t1.uc,t1.late
from tb_user
join (select user_city uc,
max(user_createdat)late
from tb_user
group by user_city)t1
on tb_user.user_city = t1.uc and
tb_user.user_createdat = t1.late
综合练习例题:
综述:两张表,一张顾客信息表customers,一张订单表orders
1、创建一张顾客信息表customers,字段要求如下:
c_id 类型为整型,设置为主键,并设置为自增长属性
c_name 字符类型,变长,宽度为20
c_age 微小整型,取值范围为0~255(无符号)
c_sex 枚举类型,要求只能在('M','F')中选择一个值
c_city 字符类型,变长,宽度为20
c_salary 浮点类型,要求整数部分最大为10位,小数部分为2位
create table customers(
c_id int primary key auto_increment,
c_name varchar(20),
c_age tinyint unsigned,
c_sex enum("M","F"),
c_city varchar(20),
c_salary float(12,2)
);
在表中任意插入3条记录,c_name为"Zhangsan","Lisi","Wangwu", c_city尽量 写"Beijing","Shanghai" ......
insert into customers values
(1,"Zhangsan",25,"M","Beijing",8000),
(2,"Lisi",30,"F","Shanghai",10000),
(3,"Wangwu",27,"M","Shenzhen",3000);
2、创建一张订单表orders,字段要求如下:
o_id 整型
o_name 字符类型,变长,宽度为30
o_price 浮点类型,整数最大为10位,小数部分为2位
设置此表中的o_id字段为customers表中c_id字段的外键,更新删除同步
create table orders(
o_id int,
o_name varchar(30),
o_price float(12,2),
foreign key(o_id) references customers(c_id)
on delete cascade
on update cascade
);
在表中任意插入5条记录(注意外键限制)
o_name分别为"iphone","ipad","iwatch","mate9","r11",其他信息自己定
insert into orders values
(1,"iphone",5288),
(1,"ipad",3299),
(3,"mate9",3688),
(2,"iwatch",2222),
(2,"r11",4400);
3、返回customers表中,工资大于4000元,或者年龄小于29岁,满足这样条件的前2条记录
select * from customers where c_salary > 4000 or c_age < 29 limit 2;
4、把customers表中,年龄大于等于25岁,并且地址是北京或者上海,这样的人的工资上调15%
update customers set c_salary=c_salary*1.15 where c_age >= 25 and c_city in ("Beijing","Shanghai");
5、把customers表中,城市为北京的顾客,按照工资降序排列,并且只返回结果中的第一条记录
select * from customers where c_city="Beijing" order by c_salary desc limit 1;
6、选择工资c_salary最少的顾客的信息
select * from customers
where c_salary = (select min(c_salary) from customers);
7、找到工资大于5000的顾客都买过哪些产品的记录明细
select * from orders where o_id in (select c_id from customers where c_salary > 5000);
8、删除外键限制
show create table orders;
alter table orders drop foreign key orders_ibfk_1;
9、删除customers主键限制
1、删除自增长属性
alter table customers modify c_id int;
2、删除主键限制
alter table customers drop primary key;
10、增加customers主键限制c_id
alter table customers add primary key(c_id);
11.增加外键限制,restrict
alter table orders add foreign key(o_id) references customers(c_id);
12.删除外键限制
然后增加外键限制,级联动作为set null;
alter table orders add foreign key(o_id) references customers(c_id)
on delete set null on update set null;