sql一些小查询语句

来源:互联网 发布:linux嵌入式应用开发 编辑:程序博客网 时间:2024/05/19 00:43

1. 表内容:

        2005-05-09 胜

        2005-05-09 胜

        2005-05-09 负

        2005-05-09 负

        2005-05-10 胜

        2005-05-10 负

        2005-05-10 负  

    如果要生成下列结果, 该如何写sql语句?  

                   胜 负

        2005-05-09  2 2

        2005-05-10  1 2

select 时间,

SUM(case when win='' then 1 else 0 end) ,

SUM(case when win='' then 1 else 0 end) 

from tb

group by 时间

 

2. 表中有A B C三列,用SQL语句实现:

    当A列大于B列时选择A列否则选择B列

当B列大于C列时选择B列否则选择C列

select (case when a>b then a else bend),
(case when b>c then b esle c end)
from table_name

 

 

 

3. 有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记 录并按以下条件显示出来(并写出您的思路):  大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。

    显示格式:           

        语文              数学                英语          

        及格              优秀                不及格

Select case when语文>= 80 then优秀when语文>= 60 then及格else不及格 end语文,case when 数学 >= 80 then优秀when数学>= 60 then及格else不及格 end数学,case when 英语 >= 80 then优秀when英语>= 60 then及格else不及格 end英语from 表名;

4. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名

    name   kecheng   fenshu

    张三     语文       81

    张三     数学       75

    李四     语文       76

    李四     数学       90

    王五     语文       81

    王五     数学       100

王五     英语       90

 

Select distinct name fromwhere name not in(Select name from表名where fenshu <= 80);

0 0
原创粉丝点击