mysql 嵌套查询(子查询)

定义

内层查询结果 作为 外层查询的条件

语法

select … from 表名 where 条件(select …);

例子

'1、把攻击值小于平均攻击值的英雄名字和攻击值显示出来'
select name, attack from sanguo where attack < ( '外层查询'
  select avg(attack) from sanguo); '内层查询  平均攻击值'
'2、找出每个国家攻击力最高的英雄的名字和攻击值(子查询)'
select name,attack from sanguo where (country, attack) in ( '外层查询'
    select country, max(attack) from sanguo group by country);  '内层查询  国家与最高攻击力'

猜你喜欢

转载自blog.csdn.net/weixin_45875105/article/details/112764237