SQL数据查询——select

SQL数据查询——select
-----------------------------------------------------------------------------  
字符串表达式的通配符        -- 只能在使用like关键字的where子句中使用通配符)  
    %    --表示任意字符
    _    --表示单个任意字符
    []    --按中括号内字符匹配
    [^]    --非中括号内^后的字符
例子:
'% find this %'        '[a-za-z]%'        '[^f-m]%'
    另外like子句要表示 []_%^ 这些字符中,标准SQL语句时,使用escape关键字定义转义字符,如下
    查询student表中,name为以_开头的记录
    select * from student where name like '\_%' escape '\' --标准SQL中
    select * from student where name like '\_%'    --MySQL中可用 \ 作转义字符
 
一些常用的
    between ... and ...
    in( .., ..., ...... )
    any, all  
    not, and, or    --逻辑运算
    like            --用于字符串匹配,支持通配符
    distinct|[all]    --指定检索独有的列值无重复,all保留重复行
 
-----------------------------------------------------------------------------  
SELECT select_list[ INTO new_table ]  
    FROM table_source [ WHERE search_condition ]  
    [ GROUP BY group_by_expression ]                 --分组
    [ HAVING search_condition ]  
    [ ORDER BY order_expression [ ASC | DESC ] ]    --排序,可加列名,列号
 
例1:对学生成绩的查询。
    select s.sname,sc.grade from s,sc where s.sno=sc.sno
例2:查询结果只显示前n条记录*
    select top 3 sno,sname,sdept from s
例3:分组查询、并改名,若改的名中有单引号,则用双引号括起来
    select sage as '学生年龄',count(*)as '学生数'
    from s group by sage
例4:
    select teacher_id + 5 from teacher
    select * from teacher where teacher+id > 4
    select concat(t.name, 'xx') from teacher t
    select distinct name, tid from student  --去掉重复项
例5:使用CASE函数分类查询
select * ,
     case cno
        when 1 then round((grade*1.03),-1)
        when 2 then round((grade*1.04),-1)  
        when 3 then round((grade*1.05),-1)
        else round((grade*1.01),-1)
     end as 期望成绩
from sc
例6:使用LIKE运算符
    select * from 学生 where 姓名 like '%李%'
例7:使用BETWEEN运算符
    select * from 学生 where 入学成绩 between 550 and 580
例8:对查询结果分组
在'学生'表中按性别统计所有学生的最高成绩。
    select case 性别
          when 0 then '女'
          else '男'
          end as 性别,  
          max(入学成绩) as 最高成绩
    FROM 学生
    group by 性别  
注意:在使用GROUP BY子句时,select子句中每一个非聚合表达式内的所有列都应包含在GROUP BY列表中。否则将会返回错误信息。
 
SQL92标准的连接查询的外连接
    使用*或+表示外连接
    select * form t1, t2 where t1.id =* t2.id -- 右外连接  
SQL99标准的连接查询
    cross join  交叉连接(广义笛卡尔积)
    natural join    自然连接(以同名列为准,无同名列则成广义笛卡尔积)
    join中使用using指定同名列
    join中用on指定连接条件
    左右全外连接分别使用left join,right join, full join
例子:
    select s.*, t.name from student s cross join teacher t;
    select s.*, t.name from student s natural join teacher t;
    select s.*, t.name from student s join teacher t using(t.id);
    select s.*, t.name from student s join teacher t on s.tid=t.id;
    select s.*, t.name from student s right join teacher t on s.tid<t.id;
 
子查询例子:
    select * from st where sname = (select sname from st where sid = 4)  
    -- ------- 除非能确保内层select只返回一个行的值,  
    -- ------- 否则应在外层where子句中用一个in限定符

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值