zoukankan      html  css  js  c++  java
  • sql查询语句

    查询表结构

    desc 表名;

    查询所有记录

    select * from 表名;

    查询指定记录

    select name,age,birthday from 表名;

    查询某些字段不同记录(去重)

    select distinct name,birthday from 表名;

    字段运算查询

    select sal*12,age from 表名;

    使用列的别名

    select sal*12 as "年工资" , age 年龄,name username from 表名;

    注意:as可以省略

    如何处理null值

    select sal*12+nvl(jiang,0) as "年工资",age from 表名;

    nvl函数:如果jiang为空时取0

    模糊查询like

    select * from 表名 where name like '李%';

    select * from 表名 where name like '李_';

    %代表多个任意字符,_代表一个任意字符

    排序

    1.升序排序,不加asc默认为添加asc

    select * from 表名 where 条件 order by 排序字段 [asc];

    2.降序排序

    select * from 表名 where 条件 order by 排序字段 desc;

    3.多重排序,先按照字段1降序排序,如果字段1有重复,则重复部分按字段2升序排序

    select * from 表名 where 条件 order by 排序字段1 desc,排序字段2;

    分组查询

    select 国家名,sum(人口),sum(面积) from 表名 group by 国家名;

    当使用group by 国家名时,属于同一个国家的一组数据将只能返回一行值,也就是说,表中所有除相同国家外的字段,只能通过sum,count等聚合函数运算后返回一个值。

    select 国家名,sum(人口),sum(面积) from 表名 group by 国家名 having sum(面积)>100000;

    这里的having其实就是之前使用的where,功能是筛选成组后的各组数据

    注意事项

    1)group by语句可以单独使用

    2)having语句只能配合group by语句使用

    3)如果在select语句中同时包含group by,having,order by那么它们的顺序是group by,having,order by

    等值多表查询

    按照等值的条件查询多个数据表中关联的数据,要求关联的多个数据表的某些字段具有相同的属性,即具有相同的数据类型、宽度和取值范围

    select a.uname,a.age,b.uanme,b.age ,b.sal   from a,b  where  a.age=b.age and  b.sal>1000;

    等值连接(内连接)

    select * from aa inner join bb on aa.aid=bb.bid;

    只返回两个表中联结字段相等的行

    等价于select * from aa,bb where aa.aid=bb.bid;

    左连接(左外连接)

    select * from aa left join bb on aa.aid=bb.bid;

    是以aa表的记录为基础的,aa可以看成左表,bb可以看成右表,left join是以左表为准的,也就是说左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录

    等价于select * from aa,bb where aa.aid=bb.bid(+);

    右连接(右外连接)

    select * from aa right join bb on aa.aid=bb.bid;

    和left join正好相反,等价于select * from aa,bb where aa.aid(+)=bb.bid;

    完全连接(全外连接) 

    select * from aa full join bb on aa.aid=bb.bid;

    只返回两个表中的所有行

    等价于

    select * from aa left join bb on aa.aid=bb.bid

    union

    select * from aa right join bb on aa.aid=bb.bid

    嵌套查询

    in可以代替‘=’

    1.简单嵌套实现

    select bumen from a where employid=(select employid from b where name='张三');

    1)(select employid from b where name='张三'

       查询出姓名是张三的员工编号

    2)select bumen from a where employid=员工编号

       查询出员工编号为10的员工所在部门

    2.嵌套修改字段数据

    update student set sal=(select sal+300 from 表名 where empno=7559) where empno=7599;

    3.并操作的嵌套查询(a与b的元素总和)

    select sal from a union select sal from b

    4.交操作的嵌套查询(属于a且属于b)

    select sal from a intersect select sal from b

    5.差操作的嵌套查询(属于a且不属于b)

    select sal from a minus select sal from b

    取表中的数据(前几条,几条到几条)

    select * from a where rownum<=2 (从数据表中取前两条记录)

    select * from (select * from scott.emp order by sal desc)a where rownum<6;(把数据表中的记录排序,取排序后的前5条记录)

    分页查询

    select * from  (select rownum rn,tb.* from ( select * from meetingroom order by roomid ) tb )a where  rn>= 1 and rn < 3

    1)首先把meetingroom 表中的数据全部正序排列
    2)再查询出行号
    3)根据行号来进行分页,如(要查出行为1到2的,二行记录)

  • 相关阅读:
    对MVC HtmlHepler控件扩展(转载)
    通过源代码研究ASP.NET MVC中的Controller和View(一)(转载)
    rasmol使用方法
    经典笑话
    直线回归的概念
    众数
    Python ImportError: No module named Tkinter
    小干扰RNA
    complementary DNA, cDNA
    反义RNA
  • 原文地址:https://www.cnblogs.com/lm970585581/p/7091780.html
Copyright © 2011-2022 走看看