select id, sum(case when result='胜' then 1 else 0 end)'胜',sum(case when result='负' then 1 else 0 end)'负' from test01 group by id;
select N.id,N.x,M.y from
(select id,count(*) as x from test01 where result='胜' group by id)N inner join
(select id,count(*) as y from test01 where result='负' group by id)M on N.id=M.id
select t1.id,t1.s1 as "胜",t2.s2 as "负" from (select id,COUNT(*) as "s1" from test01 where result="胜" group by id) t1 , (select id,COUNT(*) as "s2" from test01 where result="负" group by id) t2 where t1.id = t2.id;
2.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
select (case when A > B then A else B end),(case when B>C then B ELSE C end) from test03; 3、一个日期判断的sql语句? 请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)
select * from test03 where datediff(create_time,NOW())=0;--mysql 4、有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
select (case when A > 80 then '优秀' when A >= 60 then '及格' else '不及格' end) AS "语文",
(case when B > 80 then '优秀' when B >= 60 then '及格' else '不及格' end) AS "数学",
(case when C > 80 then '优秀' when C >= 60 then '及格' else '不及格' end) AS "英语" from test03;
select a.部门名称dname,b.业绩yj as '一月份',c.业绩yj as '二月份',d.业绩yj as '三月份'
from table2 a,table1 b,table1 c,table1 d
where a.部门dep = b.部门dep and b.月份mon = '一月份' and
a.部门dep = c.部门dep and c.月份mon = '二月份' and
a.部门dep = d.部门dep and d.月份mon = '三月份' and
select a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
sum(case when b.mon=3 then b.yj else 0 end) as '三月份',
sum(case when b.mon=4 then b.yj else 0 end) as '四月份',
sum(case when b.mon=5 then b.yj else 0 end) as '五月份',
sum(case when b.mon=6 then b.yj else 0 end) as '六月份',
sum(case when b.mon=7 then b.yj else 0 end) as '七月份',
sum(case when b.mon=8 then b.yj else 0 end) as '八月份',
sum(case when b.mon=9 then b.yj else 0 end) as '九月份',
sum(case when b.mon=10 then b.yj else 0 end) as '十月份',
sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',
sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',
from table2 a left join table1 b on a.dep=b.dep
8、华为一道面试题
一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
select A,COUNT(*) FROM test03 group by A having count(*) > 1; -- 最好
select * from (select count(A) count from test03 group by A) t where t.count > 1;