准备数据:
#user_info表
create table user_info(
id int(2) primary key,
user_name varchar(12) unique,
password varchar(15) not null,
real_name varchar(8) not null,
age int(3)
);
#address表
create table address(
id int(2) primary key,
user_id int(2) not null,
real_name varchar(8),
mobile char(11),
address varchar(150)
);
select *from address
select *from user_info
insert into user_info values(1,'浅唱灬幸福','8912@321','王晓明',12);
insert into address values(1,1,'王小明','15516472282','山西太原');
insert into address values(2,1,'王鑫','18404905139','山西大同');
insert into address values(3,1,'任建','15333021730','山西晋城');
insert into user_info values(2,'ぅ浮生若梦〤','56701wz','王楠',36);
insert into address values(4,2,'王楠','15010303314','北京海淀');
insert into address values(5,2,'赵婕','18435224278','山西长治');
insert into user_info values(3,'街角の风铃','27w4921','李晓飞',9);//无地址数据
insert into address values(6,6,'刘倩','13159775555','吉林长春');//无用户数据
理解:user_info 的信息可以理解为我们在某购物平台上注册账号所需要的信息,address 表中的数据为我们所添加的收获地址。
查询表数据:
select * from user_info
select * from address
可理解为用户名为''ぅ浮生若梦〤''的收获地址:
select * from address where user_id=(select id from user_info where user_name='ぅ浮生若梦〤')
真实姓名 姓王的收获地址:
select * from address where user_id in(select id from user_info where real_name like'王%')
#多表查询:多表之间必须有关系
#内连接,取多表之间交集
select ui.*,addr.* from user_info ui,address addr where ui.id=addr.user_id
#外连接:left right:
根据left和right关键字:left-查询出该关键字左边表的所有数据;right-查询出该关键字右边表的所有数据;
left:
select ui.*,addr.* from user_info ui left join address addr on ui.id=addr.user_id
right:
select ui.*,addr.* from user_info ui right join address addr on ui.id=addr.user_id