当前位置:Gxlcms > 数据库问题 > Mysql 基础 高级查询

Mysql 基础 高级查询

时间:2021-07-01 10:21:17 帮助过:35人阅读

对结果集列的扩展   

select*from  表名

查询多张表 查询结果 在一张表中显示

select * from info,nation  #形成笛卡尔积   缺点  查询速度慢(产生大量冗余数据)

select * from 1, 1where 1.列名=2.列名

select * from info,nation where info.code=nation.code

 

 

select info.code, info. name, birthday  from info,nation where info.code=nation.code

因为 birthday 在两张表里  没有重复  所以可以直接写  (比如简单查询里面)

 

select * from info join nation on info.nation=nation.code

 

 

二.联合查询   union

select 列名列名 from 1

union

select 列名列名 from 2

三.  子查询(查询效率高 重要)

父查询 外层查询  

子查询: 里层查询

子查询 查询出的结果作为父查询的条件

 

  1. 无关子查询  

子查询和父查询没有关系  子查询可以单独执行

父查询:select *from info where nation=()

子查询: select code from nation where name=’汉族’

select *from info where nation=(select code from nation where name=’汉族’)

②.查询系列名为‘宝马5系’的所有汽车信息
select * from car where brand=(select brand_code from brand where brand_name=‘宝马5系‘)

 

  1. 相关子查询

子查询在执行的时候 和父查询有关系   子查询不能单独执行

1.查询汽车表中 油耗小于平均油耗的所有汽车信息

父查询: 汽车的信息: select * from car where oil<平均油耗

子查询: 平均油耗;  select avg(oil) from car where brand =该系列

 select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)

 

Mysql 基础 高级查询

标签:卡尔   速度慢   查询   联合   ora   显示   表示   sel   nbsp   

人气教程排行