【MySQL】常用命令

北慕城南 提交于 2020-03-11 16:56:46
# 获取当前时间
select now();

# 获取当前时间戳
select unix_timestamp() * 1000;

# 时间转时间戳
select unix_timestamp('日期') * 1000;

# 时间戳转时间
select from_unixtime(time / 1000);

# 时间戳格式化
select from_unixtime(time / 1000, '%Y-%m-%d');

# 时间格式化
select date_format(now(), '%Y-%m-%d');

# 日期添加指定的时间间隔
adddate('日期', num);

# 模糊查询(like效率最低,条件左右亦可)
select '字段' from '表名' where '字段' like '%_%';
select '字段' from '表名' where instr('字段', '');
select '字段' from '表名' where locate('', '字段');
select '字段' from '表名' where position('' in '字段');
select '字段' from '表名' where find_in_set('', '字段');

# join用法
left join : 两个表的交集外加左表剩下的数据;
right join : 两个表的交集外加右表剩下的数据;
inner join : 两个表的交集;
cross join : 将A表的每一条记录与B表的每一条记录强行拼在一起;
using : using('字段')的功能相当于on '字段' = '字段',using会去除指定的列,而on不会;

# 分组后字段合并
select group_concat('字段') from '表名' group by '字段';

# 生成连续日期
create table date (i int(1));
insert into date (i) values (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
select
adddate('开始日期', dates.i) date
from (
	select
	d1.i + d10.i * 10 + d100.i * 100 i 
	from date d1
	cross join date d10
	cross join date d100 
) dates 
where adddate('开始日期', dates.i) <= '结束日期';

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!