闪电龟龟--笔记

万物寻其根,通其堵,便能解其困。
  博客园  :: 新随笔  :: 管理

select 查询

Posted on 2019-02-02 21:13  闪电龟龟  阅读(465)  评论(0编辑  收藏  举报

使用as给字段起别名,例如:select name as 姓名 from student;

模糊匹配(like
  "_":一个占位符。例子:select * from student where name like "_ack"; // 表示匹配name以"ack"结尾,且为四个字符的值。
  "%":匹配0个或多个任意字符。
关于null的查询:
  null在数据库中不代表没有,而是代表不知道。
  select name from student where name=null; // 这句是错误的,理解起来就是:从student中查询name等于"不知道的值",这就无法查了
  select name from student where name<>null;  // 这句也是错误的。理解起来是:从student中查询name不等于"不知道的值"。这也无法查。
  select name from student where name is null;  // 这句话是正确的,理解起来是:从student中查询name是"不知道的值"

 

范围选取(between...and...)
  select age from student where age between 10 and 20;
  等价于:
  select age from student where age>=10 and age<=20;

 

聚合函数:
  AVG():求平均值。
  Count():计算总数。
  All():默认值,表示全部。
  Max():最大值。
  ...
分组Group By
  select age,Count(*) from student Group By age; // 根据年龄进行分组,并统计各个年龄的人数。
  //
  // 值得注意的是,聚合函数不能出现在where子句中
  select age,Count(*) from student where Count(*)>1 Group By age;
  // 如果需要筛选,可以使用Having来进行过滤。
  select age,Count(*) from student Group By age Having Count(*)>1;
  //
  //
  // where不等价于group by,因为group by是对选出来的结果进行过滤的。下面进行列子说明
  select age from student where Score>90 Group By age; // 理解为:从student表中选择Score>90的age,再将age进行分组

 

查找前几行(top):
  select top 3 * from name order by age DESC; // 根据age进行降序排序查找前3行

 

不重复(distinct):
  select distinct name from student; // 选取student表中不重复的name

 

合并显示(union),默认将重复的结果去除:
  (select name from student) union (select name from teacher);  // 表示将两个表的结果合并显示。
  // union all 可以将两个或者两个以上的表的结果合并显示,默认不会将重复结果去除,也就是查询全部的意思。
  // 使用union进行关联时,关联的结果字段数量要相同,字段类型要相同。

 

数字函数
  ABS():求绝对值。
  CEILING():舍入到最大整数。
  FLOOR():舍入到最小整数。
  ROUND():四舍五入。

 

字符串函数
  len():计算字符串长度。
  lower():转换为小写。
  upper():转换为大写。
  ltrim():去除左边空格。
  rtrim():去除右边空格。
  substring():字符串截取。substring(被截取的字符串,字符串起始位置,字符串长度)

 

日期函数
  getdate():获取日期
  dateadd():增加以后的日期。例子:DATEADD(year,8,getdate()),表示在参数getdate()的基础上增加8年(在当前时间加上8年)。单位可以用year/month/day/hh(小时)/...等。
  datediff():计算两个日期之间的差额。dateiff(单位,开始日期,结束日期),单位同上有yaer等
  datepart():返回日期的部分。datepart(单位,日期参数)。单位同上;日期参数,如当前日期参数:getdate()

案列:

--select dateadd(dd,-day(getdate())+1,getdate()); -- 获取本月第一天
--select dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))

--select getdate() -- 获取当前时间
--select day(getdate()) -- 获取是当前月的第几天
select dateadd(dd,-day(getdate())+1,getdate()) -- 获取本月第一天
---------------------------------


--select datediff(m,0,getdate()) -- 第二个0表示1900-01-01。计算与1900年相差的月份,月份向下取整。例如今天是2019-03-05 13:50:45,那么获取到的值为:2019-03-01那一天以前的月份(也就是不包含3月份)
--select dateadd(mm,datediff(m,0,getdate())+1,0) -- 第三个参数的0表示1900-01-01.表示相差月份加上1900-01-01得到的日期,往往是下个月的第一天,如今天是2019-03-05 13:50:45,那么获取到的值为:2019-04-01 00:00:00.000
select dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0)) -- 在上面的基础上减去3毫秒。表示取当前月的最后一天。

day(dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0)))  -- 当前月天数

---------------------------------

 

select datediff(mm,'2018-2-15','2018-3-14');  -- 获取相差月份,这里显示1

 

 

类型转换
  cast():例子 cast('123' as int) // str转int
  convert():例子 convert(datetime, '2019-2-3') // str转datetime

 

空值处理函数(isnull):
  select isnull(name,'jr') from student; // 表示从student表中选取name字段,如果字段为空,那么用'jr'代替。

 

case函数用法:
  select name, (
  case age
  when 10 then '少年'
  when 25 then '青年'
  when 50 then '中年'
  else '不在检查年龄'
  end
  ) as 年纪类型 from student;
//
// 上面看起来并不完整,应该进行年龄段的判断。
  select name, (
  case
  when age<=10 then '少年'
  when age>10 and age<=25 then '青年'
  when age>25 and age<=50 then '中年'
  else '不在检查年龄'
  end
  ) as 年纪类型 from student;
//
// 那么如果要计算年龄类型的数量呢?

  select sex, SUM(
  case
  when age<=10 then 1
  else 0
  end
  ) as 少年, SUM(
  case
  when age>10 and age<=25 then 1
  else 0
  end
  ) as 青年, SUM(
  case
  when age>25 and age<=50 then 1
  else 0
  end
  ) as 中年
  from student group by sex;

 

 

关联查询:
  select stu.name,tea.name from student as stu join teacher as tea on stu.id=tea.sid; // 使用join
  select student.name, teacher.name from student, teacher where student.id=teacher.sid;