Mysql day2

**5,concat(参数1,参数2…)连接符:可以将多个字符串或者多个字符串列的值进行拼接
参数:可以是列,可以是字符串,可以是数字
作用:可以将多个列的值或者字符串进行拼接
注意:mysql中字符串使用的是单引号
mysql中单引号表示字符串.
语法:
select concat(‘hello’,‘world’,’’,‘tom’)
from dual;
select concat(‘hello’,concat(’
’,‘world’));

  eg:1字符串拼接: 
hello  world	


	
  eg:2字符串拼接列:
姓:last_name 	

   eg:3列拼接列:在s_emp表中
名:三 姓:张
   first_name   last_name


concat可以相互嵌套

排序 Order by
1,语法:order by 列名 排序,列名 排序;
放在select语句最后, 但是需要在limit前
升序:asc 如果不写 默认为asc
降序:desc 大到小
eg:
select id from s_emp order by id;

select id,salary from s_emp
order by id desc,salary;

注意:null 最小

2,特点:
2.1 : order by放在select语句最后一行,最后,最后,最后
2.2 : 默认升序(asc),降序(desc)
2.3 : order by后面可以跟列名、别名、select后面字段名的序号

不写排序规则就是默认升序排序
eg:



显示升序排序
eg:


显示降序排序
eg:



字符串排序是按照ASCII值排序
eg:



按照多个列排序
eg:

注意:如果按照多个列进行排序,多个列之间逗号隔开,先按照第一个列的值进行排序,如果第一个列的值相同,那么会按照第二个列的值进行排序。.......以此类推

eg: 按照commission_pct进行排序

2.4 : mysql 数据库中 null 表示无限小
select commission_pct
from s_emp
order by commission_pct desc;

where子句

作用:过滤(筛选)要查询的数据
书写位置:放置在from子句的后面 紧跟着from
书写组成:可以由字段名(列名)、常量、比较符、表达式等组成。		

1, 基本操作符 
	
     逻辑比较操作符
	=  >  <  >=  <=
	
	
	select last_name,salary
	from s_emp
	where id < 5

等于:三个都表示不等于的意思(经常用的是!=)
!= (^= mysql不支持) <>

eg:查询 id last_name salary
from s_emp 需要查询的
条件是 工资大于1000

    select id,last_name,salary
    from s_emp
    where salary > 1000

eg:查询不是41部门的员工信息
select id,last_name,dept_id
from s_emp
where dept_id != 41

2, 列 [not] between 一 and 二
在某列的值一个区间 [一,二]

eg: 查询id在[5,10]之间的员工信息
select id,last_name 
from s_emp
where id between 5 and 10;
eg:工资不在1000-2000之间的员工信息
   select id,last_name,salary
   from s_emp
   where salary not between 1000 and 2000; 

3, 列名 [not] in(41,42,43)某一列的值只能是括号里的数据
语法:where 列名 in(41,42,10)

eg:查询部门号是41或44或32中的员工信息
	select id,last_name,dept_id
	from s_emp
	where dept_id in(41,44,'32')
eg:工资不是1250|1000|1200
   select id,salary
   from s_emp
   where salary not in (1250,1000,1200)

4, and 且
需要连接多个条件

语法:where 条件一 and 条件二 当条件一和条件二都满足的数据才会被查询出来

eg:工资不在1000-2000之间的员工信息
并且员工id小于15
select id,last_name,salary
from s_emp
where salary between 1000 and 2000
and id < 15
and dept_id = 41
----10.10----
5, or 或
需要连接多个条件
eg: where 条件一 or 条件二 or 条件三 当条件一或者条件二满足一个就可以出来

部门id是41 
或者 工资大于1000  
或者 id小于5
select id,last_name,salary,dept_Id
from s_emp
where dept_Id = 41
   or salary > 1000
   or id < 5

6, (not) like:模糊匹配
_ :占位符 占一个符号位
% : 占位符 占0-多个位子
语法: where last_name like ‘tom’;
where last_name like ‘_om’;
where last_name like ‘%o%’;
escape : 定义转义符号。将特殊含义的字符串 转义为普通字符串
语法: where last_name like ‘/_briup’ escape ‘/’;

eg:last_name N开头员工信息
	select id,last_name
	from s_emp
	where last_name like 'N%'
eg:last_name 第二个字符 是 g
	select id,last_name
	from s_emp
	where last_name like '_g%'
eg:包含g
	from s_emp
	where last_name like '%g%';


插入一条sql语句
	insert into s_emp(id,last_name) values(99,'_briup');
	commit;

eg:查询下划线开头的用户信息
      select id,last_Name f 
	from s_emp 
	 where last_name like '#_%' escape '#';  
 如果like中使用到了 特殊字符,需要转移

7, 列名 is [not] null : 判断那一列是null;
语法: where commission_pct is null;

select last_name,commission_pct 
from s_emp
where commission_pct is null;

注意:mysql中字符串使用单引号,单引号中的字符区分大小写
eg:41号部门,salary 大于1500 salary降序
优先级:
(1)算术运算符

(3)比较符
(4)列名 [not]in  列名 [not]like
(5)列名 [not]between 1  and   2
(6)not
(7)and
(8)or
(函数)...

*使用括号改变优先级

函数 :单值函数 + 组函数

1,基础
位置:
可以出现在select、where、order by字句中
单值函数分类:
字符函数:
数字函数:
日期函数
转换函数
返回值 函数名(参数1,参数2,…)
select 函数()
from s_emp
where 函数()

字符串函数
 ascii(str);返回字符串 str 的第一个字符的 ascii 值(str 是空串时返回 0)
mysql> select ascii(‘2’); -> 50
mysql> select ascii(2); -> 50
mysql> select ascii(‘dete’); -> 100

 conv(参数1,参数2,参数3);
指定 参数1 是 参数二进制的数,然后转换为 参数三 进制的字符串表现输出
mysql> select conv(“a”,16,2); -> ‘1010’
mysql> select conv(“6e”,18,8); -> ‘172’
mysql> select conv(-17,10,-18); -> ‘-h’
mysql> select conv(10+“10”+‘10’+0xa,10,10); -> ‘40’

java --> a --> 1010
int a = Integer.parseInt("a",16);
String msg = Integer.toString(a,2);

 bin(n);把 n 转为二进制值并以字串返回(n 是 bigint 数字,等价于 conv(n,10,2))
 mysql> select bin(12); -> ‘1100’
 oct(n);把 n 转为八进制值并以字串返回(n 是 bigint 数字,等价于 conv(n,10,8))
 mysql> select oct(12); -> ‘14’
 hex(n);把 n 转为十六进制并以字串返回(n 是 bigint 数字,等价于 conv(n,10,16))
 mysql> select hex(255); -> ‘ff’
 char(n,…);返回由参数 n,…对应的 ascii 代码字符组成的一个字串(参数是 n,…是数字序列,null 值被跳过)
mysql> select char(77,121,83,81,‘76’); -> ‘MySQL’
 mysql> select char(77,77.3,‘77.3’); -> ‘MMM’
 concat(str1,str2,…);把参数连成一个长字符串并返回(任何参数是 null 时返回 null)
mysql> select concat(‘my’, ‘s’, ‘ql’); -> ‘mysql’
mysql> select concat(‘my’, null, ‘ql’); -> null
mysql> select concat(14.3); -> ‘14.3’
 length(str) char_length(str)返回字符串 str 的长度(对于多字节字符 char_length 仅计算一次) 
length();返回字节长度
char_length();返回字符长度

select id,last_name
from s_emp
where length(last_name) = 5

 locate(substr,str)、position(‘substr’ in ‘str’);
返回字符串 substr 在字符串 str 第一次出现的位置(str 不包含 substr 时返回 0)
这个position是locate的同义词,下面出现类似写法的都是前一个的同义词
 mysql> select locate(‘bar’, ‘foobarbar’); -> 4
mysql> select position(‘or’ in ‘hello world’); ->8
 locate(substr,str,pos);返回字符串 substr 在字符串 str 的第 pos 个位置起第一次出现的位置(str 不包含 substr 时返回 0)
 mysql> select locate(‘bar’, ‘foobarbar’,5); -> 7

 lpad(str,len,padstr);用字符串 padstr 填补 str 左端直到字串长度为 len 并返回
 mysql> select lpad(‘hi’,4,’??’); -> ‘??hi’
 rpad(str,len,padstr);用字符串 padstr 填补 str 右端直到字串长度为 len 并返回
 mysql> select rpad(‘hi’,5,’?’); -> ‘hi???’
 left(str,len);返回字符串 str 的左端 len 个字符
 mysql> select left(‘foobarbar’, 5); -> ‘fooba’
 right(str,len);返回字符串 str 的右端 len 个字符
 mysql> select right(‘foobarbar’, 4); -> ‘rbar’
 substring(str,pos,len);
返回字符串 str 的位置 pos 起 len 个字符
 mysql> select substring(‘quadratically’,5,6); -> ‘ratica’
 substring(str,pos);、substring(‘str’ from pos);返回字符串 str 的位置 pos 起的一个子串
 mysql> select substring(‘quadratically’,5); -> ‘ratically’
 mysql> select substring(‘foobarbar’ from 4); ->‘barbar’
 substring_index(str,delim,count);
返回从字符串 str 的第 count 个出现的分隔符 delim 之后的子串
(count 为正数时返回左端,否则返回右端子串)
mysql> s -> ‘www.mysql’
mysql> select substring_index(‘www.mysql.com’, ‘.’, -2); -> ‘mysql.com’
 ltrim(str) ;返回删除了左空格的字符串 str
 mysql> select ltrim(’ barbar’); -> ‘barbar’
 rtrim(str);返回删除了右空格的字符串 str
 mysql> select rtrim('barbar '); -> ‘barbar’
trim([[both | leading | trailing] [remstr] from] str);
返回前缀或后缀 remstr 被删除了的字符串
str(位置参数默认 both,remstr 默认值为空格)

  trim('str'); 删除str两端的空
  	mysql> select trim('   aaabcaaa      ');--> aaabcaaa
  trim(both 'a' from 'str');删除str两端的a
  	mysql> select trim(both 'a' from 'aaabcaaa');--> bc
  trim(leading 'a' from 'str');删除str左端的a
  	mysql> select trim(leading 'a' from 'aaabcaaa');--> bcaaa
  trim(trailing 'a' from 'str');删除str右端的a
  	mysql> select trim(trailing 'a' from 'aaabcaaa');--> aaabc
  	
注:以上每个测试的 'a' 可以不写,不写的话表示移除空格 

 soundex(str); 返回 str 的一个同音字符串(听起来“大致相同”字符串有相同的,同音字符串,非数字字母字符被忽略,在 a-z 外的字母被当作元音)
mysql> select soundex(‘hello’); -> ‘h400’
mysql> select soundex(‘quadratically’); -> ‘q36324’
 space(n); 返回由 n 个空格字符组成的一个字符串
mysql> select space(20);
±---------------------+
| space(20) |
±---------------------+
| |
±---------------------+
1 row in set (0.00 sec)
mysql> select space(10);
±-----------+
| space(10) |
±-----------+
| |
±-----------+
1 row in set (0.00 sec)
 replace(‘str’,‘oldStr’,‘newStr’); 把str中的oldStr替换为newStr
 mysql> select replace(‘hello world’,‘w’,‘m’);
±-------------------------------+
| replace(‘hello world’,‘w’,‘m’) |
±-------------------------------+
| hello morld |
±-------------------------------+
1 row in set (0.00 sec)
 repeat(‘str’,count); 重复:把str重复count次
返回由 count 个字符串 str 连成的一个字符串(任何参数为 null 时,返回 null,count<=0 时返回一个空字符串)
 mysql> select repeat(‘hel’,3);
±----------------+
| repeat(‘hel’,3) |
±----------------+
| helhelhel |
±----------------+
1 row in set (0.00 sec)
 reverse(str); 颠倒字符串 str 的字符顺序并返回
 mysql> select reverse(‘hello world’);
±-----------------------+
| reverse(‘hello world’) |
±-----------------------+
| dlrow olleh |
±-----------------------+
1 row in set (0.00 sec)
 insert(‘str’,pos,len,‘newstr’); 把newStr放到str的pos位置
 mysql> select insert(‘quadratic’, 3, 4, ‘what’); -> ‘quwhattic’
把str 中的 pos位置 开始 的len个字符替换为newstr

 elt(n,str1,str2,str3,...); 返回第 n 个字符串(n 小于 1 或大于参数个数返回 null) 
	mysql> select elt(1, 'ej', 'heja', 'hej', 'foo'); -> 'ej' 
	mysql> select elt(4, 'ej', 'heja', 'hej', 'foo'); -> 'foo' 
 field(str,str1,str2,str3,...) ; 返回 str 等于其后的第 n 个字符串的序号(如果 str 没找到返回 0) 
	mysql> select field('ej', 'hej', 'ej', 'heja', 'hej','foo'); -> 2 
	mysql> select field('fo', 'hej', 'ej', 'heja', 'hej','foo'); -> 0 
 find_in_set(str,strlist) ; 返回 str 在字符串集 strlist 中的序号(任何参数是 null 则返回 null,如果 str 没找到返回 0,参数 1 包含","时工作异常) 
	mysql> select find_in_set('b','a,b,c,d'); -> 2 
 make_set(bits,str1,str2,...) ; 把参数 1 的数字转为二进制,假如某个位置的二进制位等于 1,对应位置的字串选入字串集并返回(null 串不添加到结果中) 
	mysql> select make_set(1,'a','b','c'); -> 'a' 
	mysql> select make_set(1 | 4,'hello','nice','world'); -> 'hello,world' 
	mysql> select make_set(0,'a','b','c'); -> '' 
 export_set(bits,on,off,[separator,[number_of_bits]]);按 bits 排列字符串集,只有当位等于 1 时插入字串 on,否则插入 off(separator 默认值	",",number_of_bits 参数使用时长度不足补 0 而过长截断) 
	mysql> select export_set(5,'y','n',',',4) -> y,n,y,n 
 lcase(str);、lower(str); 返回小写的字符串 str 
	 mysql> select lcase('quadratically'); -> 'quadratically' 
 ucase(str);、upper(str); 返回大写的字符串 str 
	mysql> select ucase('quadratically'); -> ' QUADRATICALLY' 
 load_file(file_name); 读入文件并且作为一个字符串返回文件内容(文件无法找到,路径不完整,没有权限,长度大于 max_allowed_packet 会返回 null) 
	 mysql> update table_name set blob_column=load_file("/tmp/picture") where id=1; 

 数学函数
 abs(n); 返回 n 的绝对值
mysql> select abs(2); -> 2
mysql> select abs(-32); -> 32
 sign(n); 返回参数的符号(为-1、0 或 1)
 mysql> select sign(-32); -> -1
 mysql> select sign(0); -> 0
 mysql> select sign(234); -> 1
 mod(n,m); 取模运算,返回 n 被 m 除的余数(同%操作符)
 mysql> select mod(234, 10); -> 4
 mysql> select 234 % 10; -> 4
 mysql> select mod(29,9); -> 2
 floor(n) ; 返回不大于 n 的最大整数值
 mysql> select floor(1.23); -> 1
 mysql> select floor(-1.23); -> -2
 ceiling(n); 返回不小于 n 的最小整数值
 mysql> select ceiling(1.23); -> 2
 mysql> select ceiling(-1.23); -> -1
 round(n,d); 返回 n 的四舍五入值,保留 d 位小数(d 的默认值为 0)
 mysql> select round(-1.23); -> -1
 mysql> select round(-1.58); -> -2
 mysql> select round(1.58); -> 2
 mysql> select round(1.298, 1); -> 1.3
 mysql> select round(1.298, 0); -> 1
 exp(n); 返回值 e 的 n 次方(自然对数的底)
 mysql> select exp(2); -> 7.389056
 mysql> select exp(-2); -> 0.135335
 log(n); 返回 n 的自然对数
 mysql> select log(2); -> 0.693147
 mysql> select log(-2); -> null
 log10(n); 返回 n 以 10 为底的对数
 mysql> select log10(2); -> 0.301030
 mysql> select log10(100); -> 2.000000
 mysql> select log10(-100); -> null
 pow(x,y);、power(x,y); 返回值 x 的 y 次幂
 mysql> select pow(2,2); -> 4.000000
 mysql> select pow(2,-2); -> 0.250000
 sqrt(n); 返回非负数 n 的平方根
 mysql> select sqrt(4); -> 2.000000
 mysql> select sqrt(20); -> 4.472136
 pi(); 返回圆周率
 mysql> select pi(); -> 3.141593
 rand();、rand(n);
返回在范围 0 到 1.0 内的随机浮点值(可以使用数字 n 作为种子值)
 mysql> select rand(); -> 0.5925
 mysql> select rand(20); -> 0.1811
 mysql> select rand(20); -> 0.1811
 mysql> select rand(); -> 0.2079
 mysql> select rand(); -> 0.7888

 truncate(n,d) ; 保留数字 n 的 d 位小数并返回 
	 mysql> select truncate(1.223,1); -> 1.2 
	 mysql> select truncate(1.999,1); -> 1.9 
	 mysql> select truncate(1.999,0); -> 1 
 least(x,y,...);返回最小值(如果返回值被用在整数(实数或大小敏感字串)上下文或所有参数都是整数(实数或大小敏感字串)则他们作为整数(实数或大小敏感字串)比较,否则按忽略大小写的字符串被比较) 
	mysql> select least(2,0); -> 0 
	mysql> select least(34.0,3.0,5.0,767.0); -> 3.0 
	mysql> select least("b","a","c"); -> "a" 
 greatest(x,y,...); 返回最大值(其余同 least()) 
	mysql> select greatest(2,0); -> 2 
	mysql> select greatest(34.0,3.0,5.0,767.0); -> 767.0 
	mysql> select greatest("b","a","c"); -> "c" 
11.47

时期时间函数
dayofweek(date) ; 返回日期 date 是星期几(1=星期天,2=星期一,……7=星期六,odbc 标准)
mysql> select dayofweek(‘1998-02-03’); -> 3
weekday(date); 返回日期 date 是星期几(0=星期一,1=星期二,……6= 星期天)。  mysql> select weekday(‘1997-10-04 22:23:00’); -> 5
mysql> select weekday(‘1997-11-05’); -> 2
dayofmonth(date);
返回 date 是一月中的第几日(在 1 到 31 范围内)
mysql> select dayofmonth(‘1998-02-03’); -> 3
dayofyear(date);
返回 date 是一年中的第几日(在 1 到 366 范围内)
mysql> select dayofyear(‘1998-02-03’); -> 34
month(date); 返回 date 中的月份数值
mysql> select month(‘1998-02-03’); -> 2
dayname(date); 返回 date 是星期几(按英文名返回)
mysql> select dayname(“1998-02-05”); -> ‘thursday’
monthname(date); 返回 date 是几月(按英文名返回)
mysql> select monthname(“1998-02-05”); -> ‘february’
quarter(date); 返回 date 是一年的第几个季度
mysql> select quarter(‘98-04-01’); -> 2
week(date,first) ; 返回 date 是一年的第几周(first 默认值 0,first 取值 1 表示周一是周的开始,0 从周日开始)
mysql> select week(‘1998-02-20’); -> 7
mysql> select week(‘1998-02-20’,0); -> 7
mysql> select week(‘1998-02-20’,1); -> 8
year(date) ;返回 date 的年份(范围在 1000 到 9999)
mysql> select year(‘98-02-03’); -> 1998
hour(time); 返回 time 的小时数(范围是 0 到 23)
mysql> select hour(‘10:05:03’); -> 10
minute(time); 返回 time 的分钟数(范围是 0 到 59)
mysql> select minute(‘98-02-03 10:05:03’); -> 5
second(time) ; 返回 time 的秒数(范围是 0 到 59)
mysql> select second(‘10:05:03’); -> 3
period_add(p,n); 增加 n 个月到时期 p 并返回(p 的格式 yymm 或 yyyymm)
mysql> select period_add(9801,2); -> 199803
period_diff(p1,p2); 返回在时期 p1 和 p2 之间月数(p1 和 p2 的格式 yymm 或 yyyymm)
mysql> select period_diff(9802,199703); -> 11
date_add(date,interval expr type) ;、date_sub(date,interval expr type) ;、adddate(date,interval expr type);、subdate(date,interval expr type) 对日期时间进行加减法运算(adddate()和 subdate()是 date_add()和 date_sub()的同义词,也可以用运算符+和-而不是函数 date 是一个 datetime 或date 值,expr 对 date 进行加减法的一个表达式字符串 type 指明表达式 expr 应该如何被解释 [type 值 含义 期望的 expr 格式]: second 秒 seconds、minute 分钟 minutes、hour 时间 hours、day 天 days、month 月 months、year 年 years、minute_second 分钟和秒 “minutes:seconds”、hour_minute 小时和分钟 “hours:minutes”、day_hour 天和小时 “days hours”、year_month 年和月 “years-months”、hour_second 小时, 分钟, “hours:minutes:seconds”、day_minute 天, 小时, 分钟 “days hours:minutes”、day_second 天, 小时, 分钟, 秒 “days、hours:minutes:seconds” 、expr 中允许任何标点做分隔符,如果所有是 date 值时结果是一个 date 值,否则结果是一个 datetime 值) 如果 type 关键词不完整,则 mysql 从右端取值,day_second因为缺少小时分钟等于 minute_second)如果增加 month、year_month 或 year,天数大于结果月份的最大天数则使用最大天数)
mysql> select “1997-12-31 23:59:59” + interval 1 second; -> 1998-01-01 00:00:00
mysql> select interval 1 day + “1997-12-31”; -> 1998-01-01
mysql> select “1998-01-01” - interval 1 second; -> 1997-12-31 23:59:59
mysql> select date_add(“1997-12-31 23:59:59”,interval 1 second); -> 1998-01-01 00:00:00
mysql> select date_add(“1997-12-31 23:59:59”,interval 1 day); -> 1998-01-01 23:59:59
mysql> select date_add(“1997-12-31 23:59:59”,interval “1:1” minute_second); -> 1998-01-01 00:01:00
mysql> select date_sub(“1998-01-01 00:00:00”,interval “1 1:1:1” day_second); -> 1997-12-30 22:58:59
mysql> select date_add(“1998-01-01 00:00:00”, interval “-1 10” day_hour); -> 1997-12-30 14:00:00
mysql> select date_sub(“1998-01-02”, interval 31 day); -> 1997-12-02
mysql> select extract(year from “1999-07-02”); -> 1999
mysql> select extract(year_month from “1999-07-02 01:02:03”); -> 199907
mysql> select extract(day_minute from “1999-07-02 01:02:03”); -> 20102
to_days(date); 返回日期 date 是西元 0 年至今多少天(不计算 1582 年以前)
mysql> select to_days(950501); -> 728779
 mysql> select to_days(‘1997-10-07’); -> 729669
 from_days(n) ; 给出西元 0 年至今多少天返回 date 值(不计算 1582 年以前)
 mysql> select from_days(729669); -> ‘1997-10-07’
* date_format(date,format) ;

	根据 format 字符串格式化 date 值 
	(在 format 字符串中可用标志符: 
	%a	缩写星期名
	%b	缩写月名
	%c	月,数值
	%D	带有英文前缀的月中的天
	%d	月的天,数值(00-31)
	%e	月的天,数值(0-31)
	%f	微秒
	%H	小时 (00-23)
	%h	小时 (01-12)
	%I	小时 (01-12)
	%i	分钟,数值(00-59)
	%j	年的天 (001-366)
	%k	小时 (0-23)
	%l	小时 (1-12)
	%M	月名
	%m	月,数值(00-12)
	%p	AM 或 PM
	%r	时间,12-小时(hh:mm:ss AM 或 PM)
	%S	秒(00-59)
	%s	秒(00-59)
	%T	时间, 24-小时 (hh:mm:ss)
	%U	周 (00-53) 星期日是一周的第一天
	%u	周 (00-53) 星期一是一周的第一天
	%V	周 (01-53) 星期日是一周的第一天,与 %X 使用
	%v	周 (01-53) 星期一是一周的第一天,与 %x 使用
	%W	星期名
	%w	周的天 (0=星期日, 6=星期六)
	%X	年,其中的星期日是周的第一天,4 位,与 %V 使用
	%x	年,其中的星期一是周的第一天,4 位,与 %v 使用
	%Y	年,4 位
	%y	年,2 位
	mysql> select date_format('1997-10-04 22:23:00','%w %m %y'); -> 'saturday october 1997' 
	mysql> select date_format('1997-10-04 22:23:00','%h:%i:%s'); -> '22:23:00' 
	mysql> select date_format('1997-10-04 22:23:00','%d %y %a%d %m %b %j'); -> '4th 97 sat 04 10 oct 277' 
	mysql> select date_format('1997-10-04 22:23:00','%h %k %i %r %t %s %w'); -> '22 22 10 10:23:00 pm 22:23:00 00 6' 
 time_format(time,format)和 date_format()类似,但 time_format 只处理小时、分钟和秒(其余符号产生一个 null 值或 0) 
 curdate();、current_date(); 以'yyyy-mm-dd'或 yyyymmdd 格式返回当前日期值(根据返回值所处上下文是字符串或数字) 
	mysql> select curdate(); -> '1997-12-15' 
	mysql> select curdate() + 0; -> 19971215 
 curtime();、current_time(); 以'hh:mm:ss'或 hhmmss 格式返回当前时间值(根据返回值所处上下文是字符串或数字) 
	mysql> select curtime(); -> '23:50:26' 
	mysql> select curtime() + 0; -> 235026 
* now();、sysdate();、current_timestamp(); 以'yyyy-mm-dd hh:mm:ss'或 yyyymmddhhmmss 格式返回当前日期时间(根据返回值所处上下文是字符串或数字)  mysql> select now(); -> '1997-12-15 23:50:26' 
	mysql> select now() + 0; -> 19971215235026 
 unix_timestamp();、unix_timestamp(date);返回一个 unix 时间戳(从'1970-01-01 00:00:00'gmt 开始的秒数,date 默认值为当前时间) 
	mysql> select unix_timestamp(); -> 882226357 
	mysql> select unix_timestamp('1997-10-04 22:23:00'); -> 875996580 
 from_unixtime(unix_timestamp); 以'yyyy-mm-dd hh:mm:ss'或 yyyymmddhhmmss 格式返回时间戳的值(根据返回值所处上下文是字符串或数字) 
	mysql> select from_unixtime(875996580); -> '1997-10-04 22:23:00' 
	mysql> select from_unixtime(875996580) + 0; -> 19971004222300 
 from_unixtime(unix_timestamp,format); 以 format 字符串格式返回时间戳的值 
	 mysql> select from_unixtime(unix_timestamp(),'%y %d %m %h:%i:%s %x'); -> '1997 23rd december 03:43:30 x' 
 sec_to_time(seconds); 以'hh:mm:ss'或 hhmmss 格式返回秒数转成的 time 值(根据返回值所处上下文是字符串或数字) 
	mysql> select sec_to_time(2378); -> '00:39:38' 
	mysql> select sec_to_time(2378) + 0; -> 3938 
 time_to_sec(time); 返回 time 值有多少秒
	mysql> select time_to_sec('22:23:00'); -> 80580 
	mysql> select time_to_sec('00:39:38'); -> 2378 

 转换函数
 cast
 用法:cast(字段 as 数据类型)
[当然是否可以成功转换,还要看数据类型强制转化时注意的问题] 
参数二常见取值:
CHAR[(N)] 字符型
DATE 日期型 没有时间的日期,如2019-05-21
DATETIME 日期和时间型 日期时间,如2019-05-21 17:00:00
DECIMAL float型
SIGNED 有符号 Integer
UNSIGNED 无符号 Integer
TIME 时间型 去掉日期的时间,如17:00:00
eg:
select cast(start_date as time) from s_emp;
select cast(id as char) from s_emp;
select cast(start_date as signed) from s_emp;
 convert:用法:convert(字段,数据类型) 同上

	eg: 
	select convert(start_date,signed) from s_emp;

分组函数/组函数:
一组产生一个结果
1,基础:分组概念
任意一个列都可以进行分组
工资相同的人分到一个小组

 1.1:效果:分组以后,表变成什么样子了
 1.2:关键字: group by 
 1.3:书写位置:紧跟着where 后面
 1.4:语法: group by a列,b列...
 1.5:意思: 对当前结果集中的数据进行分组,对a列分组,如果a列中有相同的数据再按照b列进行分组
如果按照多个列分组,会先按照第一列分组,然后按照第二列分组...
或者说:只有在列1和列2和列3的值都相同的才会被分到一个组。
 1.6: 对组的限定: having 限定条件 
     不写group by 并且使用组函数:默认是把整个表分成一个小组

2,组函数:
2.1:基础:只有在分组的结果集中才能使用组函数
或者说有group by的sql语句才能使用组函数.
2.2:解释:使用组函数操作小组,每个小组会产生一个值
具体:
avg(列):求平均值 :在某个小组中列的平均值
count(列):计算记录总数 :在某个小组中列的记录总数
Max(列):求最大值 :在某个小组中列的最大值
Min(列):求最小值 :在某个小组中列的最小值
sum(列):求和 :在某个小组中列的和

   STDDEV(列):标准差
   VARIANCE(列):方差
2.3:理解组函数:
插入数据:
  insert into s_emp(id,last_name,dept_id) values(123,'Patel',42);
  commit;
  eg: 查询 41号部门的平均工资。
 
 select dept_id ,salary,avg(salary)
 from s_emp
 group by dept_id
 
   
  eg: 查询平均工资大于1000的部门
 

2.4:得到结论:
如果使用了组函数,那么在select中的列,必须是group by 列 不然查询不到。
2.5:对表的多个列进行分组:
eg:对last_name,和工资进行分组

解释:对last_name和salary进行分组,如果有某几条数据last_name和salary值相同那么这几条数据就会被分到同一个组。

3,代码执行顺序:
3.1:顺序:from—>where–>group by分组–>执行组函数–>having筛选->select–>order by
3.2:组函数出现的位置 : select字句 having字句 order by字句
4,组函数练习:
4.1:查询所有员工的平均工资?
【特例:把表中所有数据分到一个组不用写group by】

4.2:查询每个部门的平均工资?
	
4.3:查询平均工资大于1500的部门?

4.4:不能在GROUP BY子句中使用select列别名。
	
	
4.5:查询每个部门的平均工资并且显示出部门的名字和部门id?
4.6:查询平均工资大于1400的部门id,并且显示出部门的名字?
4.7:查询title中不包含vp字符串的每个职位的平均薪水,并对平均薪水进行降序排列,并且每个职位的总薪水大于5000?

4.8:查询s_emp表中最大工资数,并且显示出这个人的名字?**
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值