mysql _01

-- where 中不可以使用别名, 因为where先于select执行
以下是错误的SQL:
select 
    code,
    continent cont,
    name,
    population pop 
from 
    country 
where 
    cont = 'asia';
    
执行顺序是 : from -> where -> select 

-- 亚洲国家人总数大于500万
select 
    name,
    population pop 
from 
    country 
where 
        continent = 'asia'
    and 
        population > 5000000
order by 
    pop desc;
    
执行顺序是: from -> where -> select -> order by 

  1. 确定基表 决定 from 
  2. 是否要所有数据, 决定where
  3. 判断需要哪些列 决定 select 
  4. 虚表结果是否需要重排 决定 order by

 

-- 注意点 : 一旦使用了表的别名, 表的原名就不能再使用了, 因为from最先执行, 且把表名改掉了. 
-- 错误SQL
select 
    city2.name as cityName,
    city2.population cityPop,
    country2.name countryName,
    co.surfacearea,
    co.population countryPop
from 
    city2 as ci, 
    country2 co 
where 
    countrycode = code;

评论

暂无评论