AI智能
改变未来

mysql多表联合查询


创建表a插入数据

create table a(`id` int(11) primary key ,`name` varchar(6) not null ,`age` int(4) not null);insert into a values (1,\'111\',20);insert into a values (2,\'222\',20);insert into a values (3,\'333\',20);insert into a values (4,\'444\',20);

创建表b插入数据

create table  b(`id` int(11) primary key ,`result` float);insert into b values (1,11);insert into b values (2,22);insert into b values (5,11);

.内连接:INNER JOIN 求两个表的交集

select a.id,name,age,resultfrom a inner join bon a.id = b.id;
id name age result
1 1 111 20 11
2 2 222 20 22

LEFT JOIN 求两个表的交集外加左表剩下的数据

select a.id,name,age,resultfrom a left join bon a.id = b.id;
id name age result
1 1 111 20 11
2 2 222 20 22
3 3 333 null null
4 4 444 null null

RIGHT JOIN 两个表的交集外加右表剩下的数据

select a.id,name,age,resultfrom a right join bon a.id = b.id;
id name age result
1 1 111 20 11
2 2 222 20 22
3 null null null 11
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » mysql多表联合查询