简单的数据查询

一. 投影操作

1.投影:是创建一个新的关系,是临时的,不存在于原始数据中

2.select  列名列表  from 表名;

    select *from 表名;            *  通配符表示匹配所有  在这里表示查询表中所有

    select   studengID ,ExamID from  Studentexam   表示从Studentexam   表中查询 studengID  ExamID  两列

3表名前缀

   select professor.Name  from  professor

4.列别名

   select   Name As studentname, studentID As ID from student  表示将student表中的Name列的名改为 studentname,studentID列的名字改为  ID

5.计算列

   select   t_age+10  from  表名; 

6.排除重复数据

   select distinct  列A  from  表;

7.返回限定行数的查询

   select *from  gongsi where t_id>=3  AND  t_id<=3    返回2~3行

   select*from 表名 limt   开始序号,返回的行数       如果有两个参数  第一个是起始行,第二个是限定返回几行

   select*from 表名 limt  2    返回前两条

   limi 序号是从0开始   limt只能用于MySQL

8.合并列

   select  concat (t_name,'_',t_address)As '公司名字和地址' from gongsi;

二.选择操作

  1.单条件选择操作

    select 列A, 列B  from 表where 列C=值;

     select  Name from  student where studentID=3  查询student表中  studentID为3的学生的姓名

   ^=与!=类似

2.多条件选择操作

   select  studentID, Mark,comments from studentExam where ExamID=1 and Ifpassed=1;  查询studentExam中同事满足 ExamID=1 , Ifpassed=1这两个条件的学生考试数     据

   select  studentID, Mark,comments from studentExam where ExamID=1 or  Mark>80 ;  查询studentExam中满足ExamID=1 ,Mark>80这两个条件之一 或同时满足

    select  studentID, Mark,comments from studentExam where ExamID=1 and not  Ifpassed=1; 查询studentExam中 ExamID=1 , Ifpassed!=1的学生考试数据

3.执行范围测试

    select  studentID, Mark,comments from studentExam where  sustainedOn between ‘2016-03-20’and ‘2016-03-24’;

4.定义集合关系

select 列A, 列B  from 表; where 列C=Value1 or 列c=value2;

select *from yuangong where t_age in/not  in (18,19,20);

5.模糊查询

select *from yuangong  where  t_name like '王%'  查询名字以王开头的所有信息

select *from yuangong  where  t_name like '%青' 。。。。以青结尾的

select *from yuangong  where  t_name like '_ _' 查询名字是两个字的

select *from yuangong  where  t_name like 'x%n_'  。。。以x开头 倒数第二个事n的

6.处理空值数据

   select  * from yuangong where t_sex is/ is not null;

三.排序操作

   1.select *from t_student ORDER BY s_grade DESC;   将成绩从大到小

                                                                              ........ASC;    ...............小到大

2.单列排序

   select   列A ,列B,列C  from 表   order by 列A;

3.多列排序

   select   列A ,列B,列C  from 表   order by 列A,列B,列C...;


  

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值