01.在查询语句中,FROM后可跟子查询语句(select***),起到代替数据表的作用
示例:
1 SELECT title,count(*) as t 2 FROM (select distinct emp_no,title,from_date,to_date 3 FROM titles ) 4 GROUP BY title having t>=2;
主查询语句查询的数据范围就是from后子查询语句select的检索值
02.distinct(去除重复语句)适用范围不仅可以跟在SELECT后也可放置于聚合函数中使用
示例:
1 SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles 2 GROUP BY title HAVING t >= 2
03.求某列的奇偶数情况,可以根据(列值%2 = 1或列值%2 = 0)来判断奇偶数,若为‘1’则为奇数,‘0’则为偶数
示例:
1 SELECT * 2 FROM employees 3 WHERE emp_no%2 = 1 AND last_name != 'Mary' 4 ORDER BY hire_date DESC;
其中 emp_no%2 = 1 即为选定得出奇数的值
04.SQL基本遵循SELECT →FROM→WHERE→GROUP BY→HAVING→ORDER BY→LIMIT的顺序,其中limit有两张通用方法
(1)limit a offet b-其中a代表返回几位,b代表从第几位开始【0为第一位,1为第二位,以此类推……】
(2)limit a,b-其中a代表第几位开始,b代表返回几位数据
示例:
1 select emp_no,salary 2 from salaries 3 where to_date='9999-01-01' 4 order by salary desc 5 limit 1,1;
示例limit意思为返回salary工资从高到低,第二位的数据值