MYSQL数据查询

1.单表查询

1.1 选择表中的若干列

选择表中的全部或部分列即关系代数中的投影运算。

(1)查询指定的列

(2)查询全部列

(3)查询经过计算的值

    select子句的<目标表达式> 不仅可以是表中的属性列,也可以是表达式、字符串常量、函数等;

  select Sname,2018-Sage,lower(Sdept) from students;

  

 

1.2 选择表中的若干元组

(1)消除取值重复的行

select Sno from sc order by Sno;   

 

取消值重复的行

 select distinct Sno from sc order by Sno; 

    

(2)查询满足条件的元组

    查询满足条件的元组包含比较大小、确定范围、确定集合、字符匹配、空值查询、多重条件等;

比较 =,<,>,>=,<=,!+,<>,!>,!<;NOT+上述比较运算符
确定范围 between and, not between and;
确定集合 in, not in
字符匹配 like ,not  like 
空值 is null, not null;
多重条件(逻辑运算) and ,or ,not;
查询条件 谓词

确定范围

确定范围用between ... and ... 和 notbetween.... and ...。between 后面是下限 and后面是上限(查询结果包含上限和下限)

确定集合

    select Sname,Ssex from students where Sdept in ('CM','MA');

 select Sname,Ssex from students where Sdept not in ('CM','MA');

字符匹配

谓词like可以用来进行字符匹配 语法格式如下

[not] like '<匹配字符串>' [escape '<换码字符>']

   如果like后面的匹配字符串不含通配符,则like可用等于号代替

       数据库字符集为ascll 时一个汉字需要两个'_',当字符集为GBK时只需一个;

如果查询的字符串本身含有通配符则需要使用escape<换码字符>短语进行转义或者通过‘\’来转义字符

select Cno ,Cname,Ccredit from course where Cname like 'DB\_Design';

select Cno ,Cname,Ccredit from course where Cname like 'DB/_Design' escape '/';

(3)order by 子句

posted @ 2018-02-08 10:58  傲娇的洋葱  阅读(126)  评论(0)    收藏  举报