[关闭]
@nextleaf 2018-09-06T16:50:08.000000Z 字数 3789 阅读 931

2018-09-05 工作日志

Java Oracle 数据库 MySQL 工作日志


查询数据

  1. SELECT * FROM `customers`
  2. -- 查询记录个数
  3. SELECT count(*) cust_num FROM customers;
  4. -- 查询c_email为空的记录的c_id,c_name,c_email
  5. -- 注意查询某列为空值时,使用IS NULL
  6. SELECT c_id,c_name,c_email FROM customers WHERE c_email IS NULL;
  7. -- 查询c_email不为空的记录的c_id,c_name,c_email
  8. SELECT c_id,c_name,c_email FROM customers WHERE c_email IS NOT NULL;
  9. -- 水果表中s_id=101并且f_price >=5的名称和价格
  10. SELECT f_name,f_price FROM fruits WHERE s_id='101' AND f_price>=5;
  11. -- 水果表中s_id=101102,并且f_price >=的名称和价格
  12. SELECT f_name,f_price FROM fruits WHERE s_id IN ('101','102') AND f_price> 5 AND f_name='apple';
  13. -- s_id=101s_id=102
  14. SELECT s_id,f_name,f_price FROM fruits WHERE s_id in(101,102);
  15. SELECT s_id,f_name,f_price FROM fruits WHERE s_id=101 OR s_id=102;
  16. -- 去掉重复的某一列(相对于查询结果)
  17. SELECT DISTINCT s_id FROM fruits;

多列排序

与DESC相反的是ASC(升序排序)
使用ROWNUM时,只支持<,<=和!=符号,不支持>,>=,=和between...and符号

  1. -- 排序:默认ase,降序DESC
  2. SELECT f_name FROM fruits ORDER BY f_name;
  3. SELECT f_name FROM fruits ORDER BY f_name DESC;
  4. -- 查询执行顺序:1.FROM table 2.WHERE 3.GROUP BY 4.HAVING 5.SELECT 6.ORDER BY
  5. SELECT f_name,f_price FROM fruits ORDER BY f_price DESC;
  6. -- 先按f_pirce降序,再按f_name升序排列
  7. SELECT f_name,f_price FROM fruits ORDER BY f_price DESC ,f_name;
  8. -- 分组查询,使用GROUP BY时,出现在SELECT列表中的字段,如果没有在聚合函数中,那么必须出现在GROUP BY字句中
  9. SELECT s_id,f_name,count(*) AS zhonglei FROM fruits GROUP BY s_id ORDER BY s_id;
  10. SELECT * FROM fruits GROUP BY s_id,f_name;

聚合函数

AVG():返回某列的平均值。
COUNT():返回某列的行数。
MIN():返回某列的最小值。
SUM():返回某列值的和

  1. SELECT * FROM orderitems ORDER BY o_num;
  2. -- o_num分组,查询总订单价格大于100的单号和总价格
  3. SELECT
  4. o_num,
  5. SUM( quantity * item_price ) AS orderTotal
  6. FROM
  7. orderitems
  8. GROUP BY
  9. o_num
  10. HAVING
  11. SUM( quantity * item_price ) > 100;
  12. -- count()统计查询结果的行数
  13. SELECT * FROM customers;
  14. SELECT COUNT(*) AS FROM customers;
  15. SELECT COUNT(c_email) AS NotNullEmailNum FROM customers;
  16. SELECT * FROM orderitems;
  17. SELECT o_num,COUNT(f_id) AS 行数 FROM orderitems GROUP BY o_num;
  18. SELECT o_num,SUM(quantity) AS 总质量 FROM orderitems GROUP BY o_num;
  19. SELECT o_num,SUM(quantity) AS 总质量 FROM orderitems WHERE o_num=30005;
  20. -- s_id=103的平均价
  21. SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id=103;
  22. -- 分组平均值
  23. SELECT s_id,AVG(f_price) AS avg_price FROM fruits GROUP BY s_id;
  24. -- 查找价格最高的水果(子查询)
  25. SELECT f_name,f_price FROM fruits WHERE f_price=(SELECT MAX(f_price) FROM fruits);
  26. -- 分组查询最高价格
  27. SELECT * FROM fruits ORDER BY s_id;
  28. SELECT s_id,MAX(f_price) FROM fruits GROUP BY s_id;
  29. -- 分组查询最高价格的水果(ANY可替换为SOME)
  30. SELECT
  31. s_id,
  32. f_name,
  33. f_price
  34. FROM
  35. fruits
  36. WHERE
  37. f_price = ANY( SELECT MAX( f_price ) FROM fruits GROUP BY s_id );

系统信息函数:

USER:返回当前会话的登录名
NVL函数:由于聚合函数是忽略空值的,例如sum(),avg()等,所以经常使用NVL函数进行null的转换。
例如:nvl(列值,0),当列值为null时,取值为0,当列值为非null时,取值为列值本身。

  1. select ename,NVL(comm, 0) from emp;

查询执行顺序:

  1. FROM table
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

在使用GROUP BY时,出现在SELECT列表中的字段,如果没有在聚合函数中,那么必须出现在GROUP BY字句中。


下午

关于Oracle序列

  1. -- 经常用于生成主键
  2. create sequence emp_seq start with 100 increment by 10;
  3. select emp_seq.nextval from dual;
  4. select emp_seq.currval from dual;
  5. drop sequence emp_seq;
  6. create sequence ss_seq start with 1000 increment by 100;-- 默认1,步长1
  7. select ss_seq.nextval from dual;-- 获取序列下一个值
  8. select ss_seq.currval from dual;-- 获取序列当前值
  9. drop sequence ss_seq;-- 删除序列

Oracle分页

伪列,每次必须从0开始

  1. -- 分页(仅Oracle)
  2. SELECT*FROM fruits;
  3. SELECT f_name,ROWNUM FROM fruits;
  4. -- 分页(仅Oracle)
  5. -- SELECT * FROM fruits WHERE 5<ROWNUM<10;不正确,每次必须从0开始
  6. SELECT * FROM (SELECT f_name,ROWNUM r FROM fruits) WHERE r> 5 AND r< 11;
  7. SELECT * FROM (SELECT f.*,ROWNUM r FROM fruits f) WHERE r> 5 AND r< 11;
  8. SELECT*FROM (SELECT f.*,ROWNUM r FROM fruits f) WHERE r> (2-1)*5 AND r< (2*5+1);

连接查询

  1. -- 连接查询fruitssuppliers
  2. SELECT
  3. suppliers.s_id,
  4. s_name,
  5. f_name,
  6. f_price,
  7. s_city
  8. FROM fruits,suppliers
  9. WHERE fruits.s_id = suppliers.s_id;

内连接查询

  1. -- 内连接查询fruitssuppliers
  2. SELECT suppliers.s_id,s_name,f_price FROM fruits INNER JOIN suppliers ON fruits.s_id=suppliers.s_id;
  3. SELECT orders.c_id,orders.o_num FROM orders INNER JOIN customers ON orders.c_id=customers.c_id AND orders.c_id=10001;

外连接查询

  1. SELECT c_id,c_name FROM customers;
  2. SELECT o_num,c_id FROM orders;
  3. -- 查询所有客户的订单(左外连接)
  4. SELECT customers.c_id,c_name,o_num FROM customers LEFT OUTER JOIN orders ON customers.c_id=orders.c_id ORDER BY c_id;-- OUTER关键字可忽略
  5. -- 查询所有客户的订单(右外连接)
  6. SELECT customers.c_id,c_name,o_num FROM customers RIGHT OUTER JOIN orders ON customers.c_id=orders.c_id ORDER BY o_num;-- OUTER关键字可忽略
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注