当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL学习【第六篇sql语句下】

MySQL学习【第六篇sql语句下】

2019年03月23日  | 移动技术网IT编程  | 我要评论

一.select高级用法

1.传统连接(只能内连接,取交集,效率最慢)

1.根据两张表查询张三成绩

select t1.sname,t2.mark from t1,t2 where t1.sid=t2.sid and t1.sname=’zhang3’;

2.世界上小于100人的人口城市是哪个国家的

select city.name,city.countrycode,country.name 
from city,country 
where city.countrycode=country.code 
and city.population<100;

2.natural join(自连接的表要有共同的列名字)

1.查询,人口在100以上的城市名字,和所说的语言

select city.name,countrylanguage.language from  city natural  join  countrylanguage where population > 
100

3.企业中多表连接查询(内连接)

1.都到这了,还看不懂,自杀去吧

select city.name,city.countrycode,country.name 
from city join country on city.countrycode=country.code 
where city.population<100;

 

4.外连接(反正都比传统的快)

1.略。。。。

select city.name,city.countrycode,country.name 
from city left join country 
on city.countrycode=country.code 
and city.population<100;

 

5.union(合并查询)

1.为什么用union,因为他快啊,虽然打起来贼特么麻烦,但他运行效率非常快,优化语句用就是了

#范围查询or语句
mysql> select * from city where countrycode='chn' or countrycode='usa';
#范围查询in语句
mysql> select * from city where countrycode in ('chn','usa');
替换为:

mysql> select * from city where countrycode='chn' 
union  all
select * from city where countrycode='usa' limit 10

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网