SQL语句常见问题的总结(持续更新)
语言问题
修改语言注册表\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432\ORACLE\KEY_DevSuitHome1中的NLS_LANG修改为AMERICAN_AMERICA.ZHS16GBK(这是英语)
查询当前语言select userenv('LANG') from DUAL;
易错select NEXT_DAY ('01-SEP-95',1) from dual;中的1代表周几而不是几天
查看当前数据库参数,plsql_dev中的report/dba/NLS Database Parameters
精确到小时的hire_date+8/24表示的是hire_date时间加上8小时,然后精确显示到小时
AVG函数自动忽略NULL值求平均
Cop一张表create table copy_emp as select *from employees;
当如下事件发生是,会隐式的执行Commit动作:
1、数据定义语句被执行的时候,比如新建一张表:Create Table …
2、数据控制语句被执行的时候,比如赋权GRANT …( 或者DENY)
3、正常退出iSQL*Plus 或者PLSQL DEVELOPER, 而没有显式的执行COMMIT 或者ROLLBACK语句。
NOT NI 易错点
SELECT department_id, department_name
FROM departments
WHERE department_id NOT IN (SELECT department_id
FROM employees)子查询表中有null这句话就查不出东西就是错的
当然也可以这样解决
SELECT *
FROM departments
WHERE department_id NOT IN(SELECT department_id
FROM employees
WHERE job_id = 'SA_REP'
AND department_id IS NOT NULL)
还可以这样
SELECT department_id, department_name
FROM departments d
WHERE NOT EXISTS (SELECT 'X'
FROM employees
WHERE department_id
= d.department_id)
当查询的内容有多项,其中包括有分组查询的例如sum等分组函数,但是有的内容不需要,也不能分组的时候,方法是把有分组函数的内容拿出来单独建个子查询,子查询中建立需要的分组,不能分组的就放在子查询的查询外,例如
SELECT e.employee_id,e.last_name,e.salary,e.department_id,m.avg_salary
FROM employees e, (SELECT department_id,AVG(salary) avg_salary
FROM employees
GROUP BY department_id) m
WHERE m.department_id = e.department_id
ORDER BY m.avg_salary DESC;
带有数量限制的查询举例
--Practices_18:Show the department number, department name, and the number of employees working in each department that:
--a. Includes fewer than 3 employees
SELECT d.department_id,d.department_name,COUNT(*)
FROM departments d,employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_id,d.department_name
HAVING COUNT(*)<3;
--b. Has the highest number of employees:
SELECT d.department_id,d.department_name,COUNT(*)
FROM departments d,employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_id,d.department_name
HAVING COUNT(*)>=ALL (SELECT COUNT(*)
FROM departments d,employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_id,d.department_name);
--c. Has the lowest number of employees:
SELECT d.department_id,d.department_name,COUNT(*)
FROM departments d,employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_id,d.department_name
HAVING COUNT(*)<=ALL (SELECT COUNT(*)
FROM departments d,employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_id,d.department_name);
Top-N查询
--Practices_29:Write a query to display the top three earners in the EMPLOYEES table. Display their last names and salaries
方法一:
select last_name,salary
from employees e1
where
(
select count(1)
from employees e2
where e2.salary>=e1.salary
) <=3
order by salary desc;
方法二:先排序然后利用ROWNUM取需多少数据
SELECT [column_list], ROWNUM
FROM (SELECT [column_list]
FROM table
ORDER BY Top-N_column)
WHERE ROWNUM <= N;
两个时间间隔的计算处理
精确到年月
--Practices_4:Show the last names of all employees together with the number of years
--and the number fcompleted months that they have been employed.
SELECT last_name,
TRUNC(MONTHS_BETWEEN(SYSDATE,hire_date)/12,0) "YEARS",
ROUND(MOD(MONTHS_BETWEEN(SYSDATE,hire_date),12),0) "MONTHS"
FROM employees;
精确到天:两个日期相减的结果是精确到天的,例如
Select last_name,sysdate,hire_date,sysdate-hire_date,(sysdate-hire_date)/365 from employees;
1 King 2013/8/2 13:05:57 1987/6/17 9543.54579861111 26.1467008181126
根据时间处于前半月(年)还是后半来处理数据
--Practices_12:Show all employees who were hired in the first half of the month (before the 16th of the month).
SELECT last_name,hire_date
FROM employees
WHERE ROUND(hire_date,'MONTH') = TRUNC(hire_date,'MONTH');
易错点
oraclesql左右外连接是带(+)的那边是被驱动表,显示的信息少,不带的那边是驱动表,不满足条件的也显示,所以显示的多
SELECT d.department_id,d.department_name,d.location_id,COUNT(e.employee_id)
FROM departments d,employees e
WHERE e.department_id(+) = d.department_id
GROUP BY d.department_id,d.department_name,d.location_id;
d表中不满足的也显示
思路易错点
例如查找一个工作种类在1991年前半年招聘了而且在1990年前半年也招聘了,如果用AND条件就不行,应为“同一个表”中不可能同时满足两个条件。此时应该用交集处理
SELECT job_id
FROM employees
WHERE hire_date
BETWEEN '01-JAN-1990' AND '30-JUN-1990'
INTERSECT
SELECT job_id
FROM employees
WHERE hire_date BETWEEN '01-JAN-1991'AND '30-JUN-1991';
易错:
SELECT last_name,salary,DECODE(commission_pct,null,'NO','YES') COMM ,CASE
WHEN commission_pct IS NULL THEN 'YES'
ELSE 'NO'
END COMM1
FROM employees;对的
SELECT last_name,salary,DECODE(commission_pct,null,'NO','YES') COMM ,CASE commission_pct
WHEN IS NULL THEN 'YES'
ELSE 'NO'
END COMM1
FROM employees;错的
SQL语句常见问题的总结(持续更新)的更多相关文章
- SQL语句小tips(持续更新)
统计非法数据 判断people_id是否是32为字母组成的,统计不满足要求的数据 SELECT COUNT(IF(BINARY people_id NOT REGEXP '^[0-9a-z]{32}' ...
- mybatis新手入门常见问题集(持续更新)
一.参数为集合 Q:parameterType指的的类型是集合类型还是对象? A:都可以,甚至不用在xml中指定也可以.第一,mybatis会对传入的参数进行判断是不是list或者array,第二,m ...
- 【Oracle】PL/SQL Developer使用技巧(持续更新中)
1.关键字自动大写 在sql命令窗口中输入SQL语句时,想要关键字自动大写,引人注目该怎么办呢? 一步设置就可以达成了.点击Tools->Preference->Editor,看到截图中这 ...
- Loadrunder常见问题汇总(持续更新)
1.LR 脚本为空的解决方法: 1)如果安装了IE以外的浏览器,并且IE不是默认浏览器,则无法生成录制脚本 2)如果录制脚本时IE不能打开,则需要将浏览器的IE工具高级选项中,将“启用第三方浏览器扩展 ...
- [mysql]SQL语句-新增/修改 创建时间 更新时间
SQL关键词 要注意大小写 已建表,之前没有创建时间列, 现新增1列创建时间,并设置默认值为当前时间 --添加CreateTime 设置默认时间 CURRENT_TIMESTAMP ALTER T ...
- SQL Server 使用问题解答(持续更新中)
问题一:sql server 2014不允许保存更改,您所做的更改要求删除并重新创建以下表 解答:工具-选项-不勾选组织保存要求重新创建表的更改,如下图确定.
- iOS UI、Xcode、调试、代码等常见问题总汇(持续更新中)
以前比较懒,遇到问题解决了就完事了,有些问题再次遇到时忘记了当初是怎么解决的,又要查各种资料来解决.好记忆不如烂笔头,不管简单还是复杂都记一下吧,所以决定写一篇常见问题总结,方便以后查阅.现在有点忙, ...
- Debug 常见问题总结(持续更新)
2016-9-24 1.for循环变量做参数一定要小心,嵌套一个for变量不要用同一个. 2.字符串处理要打好下标的草稿,不然很容易搞混.(方法待讨论). 3.整形比较比较容易忽略=的问题 ,> ...
- angularjs开发常见问题-1(持续更新中...)
angularJs中学习中- 1.刷新当前页面数据:$state.reload service.create(data).then(function (newItem) { flash.success ...
随机推荐
- [LeetCode] Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- bootStrap Table 如何使用
最近在使用bootStrap Table 的表格功能有一些自己的理解写下来分享一下主要用的是一个bootStrapTable 和 jquery 的混合开发 具体怎样引入bootStrap Table ...
- vue中自定义组件(插件)
vue中自定义组件(插件) 原创 2017年01月04日 22:46:43 标签: 插件 在vue项目中,可以自定义组件像vue-resource一样使用Vue.use()方法来使用,具体实现方法: ...
- poj 3264 & poj 3468(线段树)
poj 3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0 求任一区间的最大值和最小值的差 #include<io ...
- hdu5634 BestCoder Round #73 (div.1)
Rikka with Phi Accepts: 5 Submissions: 66 Time Limit: 16000/8000 MS (Java/Others) Memory Limit: ...
- bzoj1233[Usaco2009Open]干草堆tower 单调队列优化dp
1233: [Usaco2009Open]干草堆tower Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 983 Solved: 464[Submi ...
- bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2253 Solved: 1136[Submit][S ...
- C语言程序设计第二次作业1
(一)改错题 1.输出带框文字:在屏幕上输出以下3行信息. ************* Welcome ************* 源程序 include int mian() { printf(&q ...
- sb error
width: $("#StudentManagement").parent().width(), height: $("#StudentManagement") ...
- Safari 3D transform变换z-index层级渲染异常
(猛戳来源:http://www.zhangxinxu.com/wordpress/?p=5569)