case when then else end 使用学习
国家(country)人口(population)中国600美国100加拿大100英国200法国300日本250德国200墨西哥50印度250洲人口亚洲1100北美洲250其他700国家(country)性别(sex)人口(population)中国1340中国2260美国145美国255加拿大151加拿大249英国140英国260国家男女中国340260美国4555加拿大5149英国4060
普通情况下,用union也可以实现用一条语句进行查询。但是那样增加消耗(两个select部分),而且sql语句会比较长。
下面是一个是用case函数来完成这个功能的例子
select country, sum( case when sex = '1' then population else 0 end), --男性人口 sum( case when sex = '2' then population else 0 end) --女性人口 from table_a group by country;
constraint check_salary check ( case when sex = '2' then case when salary > 1000 then 1 else 0 end else 1 end = 1 )
constraint check_salary check ( sex = '2' and salary > 1000 )
?
?
?