当前位置: 移动技术网 > IT编程>数据库>Mysql > 工作中常用的mysql语句分享 不用php也可以实现的效果

工作中常用的mysql语句分享 不用php也可以实现的效果

2017年12月12日  | 移动技术网IT编程  | 我要评论

日有所诵一年级,合肥中山医院叶凡,最好的股票学习网站

1. 计算年数
你想通过生日来计算这个人有几岁了。
复制代码 代码如下:

select date_format(from_days(to_days(now()) - to_days(@dateofbirth)), '%y') + 0;

2. 两个时间的差
取得两个 datetime 值的差。假设 dt1 和 dt2 是 datetime 类型,其格式为 ‘yyyy-mm-dd hh:mm:ss',那么它们之间所差的秒数为:
unix_timestamp( dt2 ) - unix_timestamp( dt1 ) 除以60就是所差的分钟数,除以3600就是所差的小时数,再除以24就是所差的天数。
3. 显示某一列出现过n次的值
复制代码 代码如下:

select id
from tbl
group by id
having count(*) = n;

4. 计算两个日子间的工作日
所谓工作日就是除出周六周日和节假日。
复制代码 代码如下:

select count(*)
from calendar
where d between start and stop
and dayofweek(d) not in(1,7)
and holiday=0;

5. 查找表中的主键
复制代码 代码如下:

select k.column_name
from information_schema.table_constraints t
join information_schema.key_column_usage k
using (constraint_name,table_schema,table_name)
where t.constraint_type='primary key'
and t.table_schema='db'
and t.table_name=tbl'

6. 查看你的数库有多大
复制代码 代码如下:

elect
table_schema as 'db name',
round( sum( data_length + index_length ) / 1024 / 1024, 3 ) as 'db size (mb)',
round( sum( data_free ) / 1024 / 1024, 3 ) as 'free space (mb)'
from information_schema.tables
group by table_schema ;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网