数据库单表查询

1、查询表中所有信息

select * from +表名;

2、查询表中字段的信息

select 字段名1,字段名2,.....from +表名;

3、在查询结果中让字段使用别名

四种写法:select 字段 别名,字段 “别名”,字段 as “别名”,字段 as 别名;

其中as都可以不加,如果不加双引号那么别名里面不能出现空格等字符.。

4、数据库中的字符连接

在Java中使用的是加号而在数据库中使用的是||,字段的名字使用单引号。

可以将两个字段使用字段连接符连接起来。

select “字符段”||‘字符段’||‘字符段’.....form 表名;

5、去除重复

select distinct 字段名1,字段名2...form 表名;

去重操作将重复的变为只剩下一个。

6、排序

单字段排序

select * from 表名 order by 字段名 asc;升序排列 默认的就是升序 所以可以不写

select *from 表名 order by 字段名 desc;降序排列

多字段排序

select * from 表名 order by 字段名 ,字段名 ....asc;

select * from 表名 order by 字段名,字段名.... desc;

7、逻辑运算

在select后面的字段可以直接进行逻辑运算,当然字段下面的内容必须是整数,如果字段下面的内容也是字符串的话,那么可以使用拼接符将字符拼接。

select 字段+数字,字段||‘字符串’...from 表名;

---------------------------------------------------------------------------------------------------------------------------------------------------------

--使用where子句查询筛选
      --select 字段名,字段名,...from表名 where 筛选条件
      --单筛选条件
            --使用运算符进行筛选 =,>,>=,<,<=,<>  单个条件中
            --注意:如果条件中的值为字符,必须使用单引号括起来
             --查询所有的员工的工资信息
             select empno,ename,sal+comm as 薪资 from emp
             --查询SMITH的个人信息
             select * from emp where ename='SMITH'
             --查询SMITH的薪资信息,逻辑运算符=
             select empno,ename,sal,sal+comm from emp where ename='SMITH'
             --查询工资大于1000的员工信息,逻辑符>
             select * from emp where sal>'2000'
             --查询工资不等于3000的员工信息
             select * from emp where sal<>3000 order  by sal
         --练习:
             --查看工资等于1250的员工信息
             select *from emp where sal='1250'
             --查看工作等于CLERK的员工信息
              select * from emp where job='CLERK'
             --查看工资大于1250的员工姓名和工作
             select ename,job from emp where sal>1250
             --查看工资大于等于2000的员工信息
             select * from emp where sal>=2000;
             --查看工资小于等于2000的员工信息;
             select * from emp where sal<=2000;
             --查看工资不等于1500的员工信息
             select * from emp where sal<>1500;
             --查看入职日期在81年后的员工信息
                   --注意:oracle默认的日期格式为 日-月-年,示例'03-1月-1981'
             select * from emp order by hiredate
             select * from emp where hiredate>='01-1月-1981' order by hiredate
       --多条件筛选(where子句关键字:and,or,like,is null,is not null, in ,between and)
             --查询工资在2000-3000之间的员工信息
               --使用and关键字,多条件同时成立的筛选使用and关键字进行条件连接
               select * from emp where sal>=2000 and sal<3000
               --使用between  and 关键字进行条件连接,包含两头的数据
               select * from emp where sal between 2000 and 3000
             --查询工作为SALESMAN,ANALYST,MANAGER的员工信息
               --使用or关键字,进行或条件的筛选。
              select * from emp where job='SALESMAN' or job='ANALYST' or job='MANAGER' order by job
               --使用in关键字,也可以进行或筛选,但是in中的内容只能为一个字段的值。
              select * from emp where job in('SALESMAN','ANALYST','MANAGER')
             --查询姓名中包含s的,以s开头的,以s结尾的,第二个字符为A的。(模糊查询)
                    --%号表任意多个的任意字符
                    --select * from 表名 where 字段名 like '%字符%' 查询包含指定字符的数据
                    select * from emp where ename like '%S%'  --包含s的
                     --select * from 表名 where 字段名 like '字符%' 查询以指定字符开头的数据
                    select * from emp where ename like 'S%'--以S开头
                     --select * from 表名 where 字段名 like '%字符' 查询以指定字符结尾的数据
                    select * from emp where ename like '%S'--以S结尾的
                     --select * from 表名 where 字段名 like '_字符%' 查询指定位置为指定字符的数据
                          --_表示一个任意字符
                    select * from emp where ename like '_A%'--第二个字符为A的
                     --select * from 表名 where 字段名 like '%字符2字符1%' escape'字符2' 
                         --escape将指定的字符变为转义字符
                         --转义字符可以将特殊字符转为普通字符
                    select * from emp where ename like '%/_%' escape '/'
                    select * from emp for update
             --查询有津贴的员工信息
                    -- select * from 表名 where 字段名 is null 字段值为null
                    -- select * from 表名 where 字段名 is  not null 字段值不为null
                    --多个条件使用and关键进行连接,筛选的是符合所有条件的数据
                            --select * from 表名 where 筛选条件1 and 条件2 and ....
                    select * from emp where comm is not null and comm>0 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值