星空网 > 软件开发 > 数据库

sql 查询基本语法

1、计算列
       select * from emp
          --*表示所有的
          --from emp 表示从emp表查询
 
select empno,ename from emp;
 
select ename,sal*12 as "年薪" from emp
      --as可以省略,记住“年薪“不要写成‘年薪’,也不要写成 年薪,方便移植
 
select ename,sal*12 as "年薪",sal "月薪",job from emp
 
select 5 from emp;
     --OK ,输出一个常量值
     --输出的行数是emp表的行数,每行只有一个字段,值是5
select 5;
     --ok
     --不建议使用,没什么实际意义 
 
注意:
        在Oracle中字段的别名不允许用单引号括起来
        但是SqlServer 2005中却允许
        因此为了兼容性,最好字段别名用双引号括起来,不要用单引号
 
 
2、distinct【不允许重复的】
select distinct deptno from emp;         --distinct deptno 会过滤掉重复的deptno值
 
select distinct comm from emp;         --distinct可以过滤掉重复的null,或者说如果有多个null,会把一个输出
 
 select distinct comm,deptno from emp;          --distinct把comm和deptno的组合进行过滤,查出两两互不相同的记录,
 
select distinct deptno,comm from emp
--含义:把deptno和comm的组合都不重复的输出
 
select deptno,distinct comm from emp --error,deptno的元素多comm中的元素少
 
 select deptno,distinct comm from emp;             --error,逻辑上有冲突
 
学习方式:要多思考
 
3.between【在某个范围之内】
 
--查找工资在1500到3000之间(包括1500到3000)的所有员工的信息
select * from emp where sal between 1500 and 3000;
--等价于
select * from emp where sal>=1500 and sal<=3000;
 
--查找工资小于1500或者大于3000(包括1500到3000)的所有员工的信息
select * from emp where sal<1500 or sal>3000;
 
select * from emp where sal not between 1500 and 3000;
 
4.in【属于若干个孤立值】
 
--把sal=1500 和sal=3000的元素输出
select * from emp where sal in (1500,3000);
--等价于
select * from emp where sal=1500 or sal=3000
 
--把sal不等于1500 和sal不等于3000的元素输出
select * from emp where sal not in (1500,3000);
--等价于
select * from emp where sal!=1500 and sal!=3000
select * from emp where sal<>1500 and sal<>3000
  --数据库中不等于有两种表示:!=,  <>,建议使用第二种
  --多或取反是并且 非并且取反是或
 
 
5、top:【最前面的若干条诗句】可用于分页
select top 2 * from emp;       --筛选出前两行数据
select top 2 from emp;       --error
select top 15 percent * from emp; --如果百分比不是整数则向上取整
 
--把工资在1500到3000 之间(包括1500和300)的员工中工资最高的前4个人的信息输出
select top 4 * from emp where sal between 1500 and 3000 order by sal
--执行过程 from哪个表->where->order排序->top->所有字节
--desc 降序,不写则默认是升序(asc)
 
6、null【没有值,空值】
    ①、零和人null是不一样的,null表示空值,没有值,零表示一个确定的值
    ②、null 不能参与如下的运算:<>  !=  =
    ③、null可以参与如下的运算: is       not is
--输出奖金非空的员工的信息-
select * from emp where comm <> null; --输出为空
select * from emp where comm !=null; --输出为空
select * from emp where comm=null; --输出为空error
  --总结:null不参与<> != = 运算
--null可以参与is not is
select * from emp where comm is null; --输出奖金为空的员工的信息
select * from emp where comm is not null; --输出奖金不为空的员工的信息
 
    ④、任何类型的数据都允许是null
            create table t1(
                name nvarchar(20),
                cnt int,
                riqi datetime
        )
        insert t1 values(null,null,null);
        select * from t1;
 
        ⑤、任何数字与null参与数学运算的结果永远是null
--输出每个员工的姓名 年薪(包含奖金) comm假设是一年的奖金
select empno, ename,sal*12+comm "年薪" from emp;
--此程序证明,任何数字与null参与数学运算的结果永远是null
 
--正确的写法是:
select empno, ename,sal*12+isnull(comm,0) "年薪" from emp;
 --isnull(comm,0)如果comm是null 就返回零,否则返回comm的值
 
7、order by【以某个字段排序】                        
        order by a,b                --a和b都是升序
        order by a,b desc        --a升序,降序
        order by a desc,b        --a降序,b升序
        order by a desc ,b desc    --a降序,b降序
        
        文字描述
                如果不指定排序的标准,则默认是升序,升序用asc表示,默认可以不写
                为一个字段指定的排序标准并不会对另外一个字段产生影响
 
        强烈建议为每一个字段都指定排序的标准
 
例子:
--asc是升序的意思,默认可以不写,desc是降序
select * from emp order by sal --默认是按照升序排序
select * from emp order by deptno,sal; -- 先按照deptno升序排序,如果deptno相同,再按照sal升序排序
select * from emp order by deptno desc,sal ;
  --先按照deptno降序排序,如果deptno相同,再按照sal升序排序,
  --记住sal是升序,不是降序
  --order by a desc,b,c,d desc 只对a产生影响, 不会对后面的b、c、d产生影响
 
 select * from emp order by deptno,sal desc;
 --问题:desc是否会对deptno产生影响?
 --答案:不会、
 --先按deptno升序,如果deptno相同,再按sal降序
 
 
8、模糊查询
格式:
  select 字段的集合 from 表名 where 某个字段的名字 like匹配的内容

  匹配的条件通常含有通配符

%
表示任意0个或者多个字符
select * from emp where ename like '%A%';  --ename 只要含有字符A就输出
select * from emp where ename like 'A%'; --ename 只要首字母是A的就输出
select * from emp where ename like '%A'; --ename 只要未字母是A的就输出
_[这是下划线 不是减号]
表示 单个字符
select * from emp where ename like '_A';--ename只要第二个字母是A的就输出
[a-f]
a到f中的任意单个字符,只能是a b c d e f中的任意一个
select * from emp where ename like '_[A-F]%'; 
--把ename中第二个字母是A或B或C或D或E或F的数据输出

 

[a,f]
a或f

 

[^a-c]
不是a,也不是b,也不是c的任意单个字符
select * from emp where ename like '_[^A-F]%';
--把ename中第二个字母不是A也不是B也不是C也不是D也不是E也不是F的数据输出


注意:
   匹配的条件必须用单引号括起来   不能省略  也不能改用双引号

   通配符
通配符作为不同字符使用的情况

  select * from 表明 where name like '%\%%'  escape '\'   --把name中包含有%的输出
  select * from 表明 where name like '%m%%'  escape 'm' // escape 后面的内容表明在其之后的元素当做转义字符




原标题:sql 查询基本语法

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊直评:https://www.ikjzd.com/w/231
美国社交电商Passfeed:https://www.ikjzd.com/w/2310
韩国综合性网站Auction:https://www.ikjzd.com/w/2311
韩国电商WeMakePrice:https://www.ikjzd.com/w/2312
91出口网:https://www.ikjzd.com/w/2313
壹米滴答:https://www.ikjzd.com/w/2314
一夜之间大批卖家货物被偷,又迎大量Listing将被下架!跨境赚钱"好日子"到头了?:https://www.goluckyvip.com/news/220219.html
晚上沈阳市区哪里好玩的地方 晚上沈阳有什么好玩的地方:https://www.vstour.cn/a/410235.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流