$ vim /usr/local/etc/my.cnf
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
set @@sql_mode
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
比如索引idx_a_b_c(a,b,c),相当于创建了(a)、(a,b)、(a,b,c)三个索引,后二者为冗余索引:①where a=? 用到(a) ⑦where c=? 用不到②where b=? and a=? 用到(a,b) ⑧where b=? and c=? 用不到③where a=? and c=? 用到(a)④where a>? and b=? 用到(a)⑤where a=? and b=? and c>? 用到(a,b,c)⑥where a=? and b>? and c=? 用到(a,b)
SELECT * FROM tel_record t1 INNER JOIN (SELECT id FROM tel_record WHERE qiye_id = xxx ORDER BY id DESC LIMIT 999900,100) t2 ON t1.f_id = t2.f_id;程序端保留当前页的最小id、最大id(id是主键),降序情况下,每次提取下一页的数据时,id < min_id order by id desc limit 100; 上一页 id > max_id order by id desc limit 100
(一). 在子查询中使用 NULL 仍然返回结果集 select * from TableIn where exists(select null) 等同于: select * from TableIn (二). 比较使用 EXISTS 和 IN 的查询。注意两个查询返回相同的结果。 select * from TableIn where exists(select BID from TableEx where BNAME=TableIn.ANAME) select * from TableIn where ANAME in(select BNAME from TableEx) (三). 比较使用 EXISTS 和 = ANY 的查询。注意两个查询返回相同的结果。 select * from TableIn where exists(select BID from TableEx where BNAME=TableIn.ANAME) select * from TableIn where ANAME=ANY(select BNAME from TableEx) NOT EXISTS 的作用与 EXISTS 正好相反。如果子查询没有返回行,则满足了 NOT EXISTS 中的 WHERE 子句。
结论: EXISTS(包括 NOT EXISTS )子句的返回值是一个BOOL值。 EXISTS内部有一个子查询语句(SELECT … FROM…), 我将其称为EXIST的内查询语句。其内查询语句返回一个结果集。 EXISTS子句根据其内查询语句的结果集空或者非空,返回一个布尔值。
在插入记录前,需要检查这条记录是否已经存在,只有当记录不存在时才执行插入操作,可以通过使用 EXISTS 条件句防止插入重复记录。 INSERT INTO TableIn (ANAME,ASEX) SELECT top 1 ‘张三’, ‘男’ FROM TableIn WHERE not exists (select * from TableIn where TableIn.AID = 7)
not in和not exists的区别: not in 只有当子查询中,select 关键字后的字段有not null约束或者有这种暗示时用not in,另外如果主查询中表大,子查询中的表小但是记录多,则应当使用not in, 例如:查询那些班级中没有学生的, select * from class where cid not in(select distinct cid from stu) 当表中cid存在null值,not in 不对空值进行处理 解决:select * from class
where cid not in
(select distinct cid from stu where cid is not null)
not in的执行顺序是:是在表中一条记录一条记录的查询(查询每条记录)符合要求的就返回结果集,不符合的就继续查询下一条记录,直到把表中的记录查询完。也就是说为了证明找不到,所以只能查询全部记录才能证明。并没有用到索引。 not exists:如果主查询表中记录少,子查询表中记录多,并有索引。 例如:查询那些班级中没有学生的, select * from class2
where not exists
(select * from stu1 where stu1.cid =class2.cid)
not exists的执行顺序是:在表中查询,是根据索引查询的,如果存在就返回true,如果不存在就返回false,不会每条记录都去查询。 之所以要多用not exists,而不用not in,也就是not exists查询的效率远远高与not in查询的效率。
实例:
exists,not exists的使用方法示例,需要的朋友可以参考下。
学生表:create table student ( id number(8) primary key, name varchar2(10),deptment number(8) )
分析:如果有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id
and sc.course_id=c.id存在null,
这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程,
此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。
SQL> select * from t_student ts where not exists (select * from course c where not exists (select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));
ID NAME DEPTMENT_ID ---------- --------------- ----------- 1 echo 1000
2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id =course.id 有一条为空,即not exists null 为true,此时select * from course有查询结果(id为子查询中的course.id ),
因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。
SQL> select id,name from t_student where exists
(select * from course where not exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME ---------- --------------- 2 spring 3 smith 4 liter
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;
select id,name from t_student 不执行查询。
SQL> select id,name from t_student where not exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME ---------- --------------- 4 liter
4.查询至少选修了一门课程的学生。 SQL> select id,name from t_student where exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME ---------- --------------- 1 echo 2 spring 3 smith
SELECT OrderID, Quantity, CASE WHEN Quantity > 30 THEN “The quantity is greater than 30” WHEN Quantity = 30 THEN “The quantity is 30” ELSE “The quantity is under 30” END FROM OrderDetails;