MYSQL多表查询
2017-01-01 本文已影响0人
钟小胖子
多表查询就是从多张表中查找所需数据,并且整合在一起显示出来的意思
多表查询拥有多种实现方式:内连接、外连接、子查询
一、内连接查询——inner join
select ID,city.name,city.population,lifeexpectancy from city,country where id<10 and city.countrycode=country.code;
标准写法:
select ID,city.name,city.population,lifeexpectancy from city inner join country where id<10 and city.countrycode=country.code;
补充:笛卡尔积(由全匹配所导致的问题)
select join1.id,name,city.unit from join1,join2;
二、外连接查询 outer join ,left and right
select ID,city.name,city.population,lifeexpectancy from city LEFT OUT JOIN country
ON id<10 and city.countrycode=country.code limit10;
左右连接的区别就是“以谁为准”