回覆列表
-
1 # 用戶6437608732773
-
2 # 用戶698932142283960
select 性別,case when 年齡 between 20 and 29 then 1 else 0 end [20-29],case when 年齡 between 30 and 39 then 1 else 0 end [30-39],case when 年齡 between 40 and 49 then 1 else 0 end [40-49]from 表名group by 性別以上使用於大部分數據庫只是在起別名上,只適用於sqlserveroracle的話起別名select 性別,case when 年齡 between 20 and 29 then 1 else 0 end "20-29",case when 年齡 between 30 and 39 then 1 else 0 end "30-39",case when 年齡 between 40 and 49 then 1 else 0 end "40-49"from 表名group by 性別mysql的話select 性別,case when 年齡 between 20 and 29 then 1 else 0 end `20-29,case when 年齡 between 30 and 39 then 1 else 0 end `30-39`,case when 年齡 between 40 and 49 then 1 else 0 end `40-49from 表名group by 性別
樓上說的答非所問啊,人家是建表,你給個查詢出來
比如建立個學生表,裡邊包括,學號,姓名,性別,其他字段略
create table 學生表
(
學號 char(5) not null primary key,
姓名 varchar(10) not null,
性別 char(2) default "男" check (性別 in ("男","女"))
)
其中default "男" 就是默認為男
check (性別 in ("男","女") 就是性別只可以為男或女