MySQL从4.1版本开始支持子查询,以提升查询的灵活度。css
子查询:把内层查询结果看成外层查询的比较条件。html
缺点
执行子查询时,MYSQL须要建立临时表,查询完毕后再删除这些临时表,因此,子查询的速度会受到必定的影响,这里多了一个建立和销毁临时表的过程。
优化
使用链接查询代替子查询,链接查询不须要创建临时表,所以其速度比子查询快。mysql
两表都有该数据的记录。web
select a.id
from table1 a, table2 b
where a.a_id=b.a_id
等价于sql
select a.id from table1 a inner join table2 b on a.a_id=b.a_id
左表全部记录 + 右表关联到的记录。数据库
右表全部记录 + 左表关联到的记录。app
查只在左表中有、不能在右表中存在的数据svg
select a.name from a where a.name not in (select name from b)
或
select a.name from a where not exists(select 1 from B where a.name=b.name)
或
select a.name
from a
left join b on a.name=b.name
where b.name is null
MySQL 使用下列数据类型在数据库中存储日期或日期/时间值:函数
函数 | 用途 | 举例 |
---|---|---|
curdate() | 当前日期 | 2017-11-29 |
curtime() | 当前时间(累加执行时间) | 09:32:32 |
now() | 语句开始执行时的日期和时间 | 2017-11-29 09:32:32 |
sysdate() | 实时的系统日期和时间(累加执行时间) | 2017-11-29 09:32:32 |
date_format() | 格式化日期/时间 | |
extract() | 提取想要部分 | |
date() | 提取日期部分 | 2017-11-29 |
hour() | 提取小时部分 | 09 |
to_days() | 返回天数(从公元 0 年开始) | |
dayOfYear() | 返回一年中第几天(1-366) | |
dayOfMonth() | 返回本月中第几天(1-31) | |
dayOfWeek() | 返回本周中第几天(1周日-7周六) | |
date_add() | 日期添加指定间隔 | |
date_sub() | 日期减去指定间隔 | |
from_unixtime(unix_timestramp) | 将Unix 时间戳转为日期格式 | 2017-11-29 09:32:32.6560 |
from_unixtime(unix_timestramp, format) | 格式化’%Y-%m-%d %h:%i:%s’ |
注意:now() 和 curtime()、sysdate() 返回的 Time 多是不同的,由于 curtime()、sysdate() 返回的时间会 累加 执行时间,而 now() 返回的时间永远都是开始执行时的时间:
实际使用时,通常用 now() ,由于用 sysdate() 可能致使主库和从库执行时返回值不同,致使主从数据库不一致。
发布后格式与编辑预览时是不一致,本身看的话,仍是先点编辑再看好了。
select * from dg_user where date_format(updateTime, '%Y-%m-%d') = date_format(NOW(), '%Y-%m-%d');
select * from dg_user where updateTime >= date_format(NOW(),'%Y-%m-%d');
SELECT * FROM dg_user WHERE DATEDIFF(updateTime, NOW()) = 0;
快速参考使用
-- %Y:年(4 位);%m:月(00-12);%d:日,月的天(00-31);
-- %T:时间, 24-小时 (hh:mm:ss);
-- %H:小时(00-23);%i:分钟(00-59);%s:秒(00-59);%f:毫秒;
select now(), date_format(now(), '%Y-%m-%d %T') from dual;
select now(), date_format(now(), '%Y-%m-%d %H:%i:%s') from dual;
select year(curdate()), month(curdate()), day(curdate()), dayofyear(curdate()), weekofyear(curdate());
select DATE_SUB(curdate(), interval day(curdate()) - 1 day);
select last_day(curdate());
select day(LAST_DAY(now()));
select dayOfMonth(LAST_DAY(now()));
语法
DATE_ADD(date, INTERVAL expr type);
DATE_SUB(date, INTERVAL expr type);
时间 +/- INTERVAL expr type;
-- date 参数是合法的日期表达式;
-- expr 参数是您但愿添加的时间间隔;
-- type 参数是时间单位;
使用
-- 前 7 天
select * from dg_user where date(updateTime) >= date_sub(curdate(), interval 7 day) select * from dg_user where date(updateTime) >= curdate() - interval 7 day
-- 昨天
select * from dg_user where to_days(updateTime) = to_days(CURDATE()) - 1;
select * from dg_user where DAYOFYEAR(updateTime) = DAYOFYEAR(now()) - 1;
SELECT * FROM dg_user WHERE DATEDIFF(updateTime, CURDATE()) = -1;
select * from dg_user where updateTime between date_sub(now(), interval 1 day) and CURDATE();
-- 今天
select * from dg_user where to_days(updateTime) = to_days(CURDATE());
SELECT * FROM dg_user WHERE DATEDIFF(updateTime, CURDATE()) = 0;
-- 明天
select * from dg_user where to_days(updateTime) = to_days(CURDATE()) + 1;
SELECT * FROM dg_user WHERE DATEDIFF(updateTime, CURDATE()) = 1;
-- 查看今天周几:0周一 ~ 6周日
SELECT WEEKDAY(now());
-- 上周
select * from dg_user where yearweek(date(updateTime)) = yearweek(now()) - 1;
select * from dg_user where yearweek(date_format(updateTime, '%Y-%m-%d')) = yearweek(now()) - 1;
select * from dg_user where period_diff(date_format(updateTime, '%Y%m'), date_format(curdate(), '%Y%m')) = -1;
-- 本周
select * from dg_user where yearweek(date_format(updateTime, '%Y-%m-%d')) = yearweek(now());
-- 上一月
select * from dg_user where MONTH(updateTime) = MONTH(now()) - 1;
-- 当前时刻的上一月的时间
select now(), date_sub(now(), INTERVAL 1 YEAR_MONTH), date_sub(now(), INTERVAL 1 MONTH), now() - INTERVAL 1 MONTH;
-- 本月
select * from dg_user where date_format(updateTime, '%Y-%m') = date_format(curdate(), '%Y-%m');
-- 上一季
select * from dg_user where QUARTER(updateTime) = QUARTER(now()) - 1;
-- 本季
select * from dg_user where QUARTER(updateTime) = QUARTER(now());
-- 上一年
select * from dg_user where year(updateTime) = year(date_sub(now(), interval 1 year));
-- 本年
select * from dg_user where year(updateTime) = year(now());