bitscn.com
6条常用的mysql查询
使用结构化查询语言,以制定正确的,有效的数据库的问题和命令查询建筑艺术是一门艺术。在select查询中,你可以使用join,where和having子句范围的结果以特定的行和列,group by将结果行分析总结,和union组合多个查询的结果。insert,delete和update命令可以参考的连接。insert ... select的查询结果插入到另一个表中。删除和更新范围内的where子句。
1、在多大的年龄
知道出生日期,需要计算这个人多少岁。假设你的出生日期是1986/05/25:
select date_format(from_days(to_days(now()) - to_days(19860525)), '%y') + 0;
2、两个日期之间的差异
在几秒钟,几分钟,几小时或几天的查找两个日期时间值之间的差异。如果dt1和dt2是datetime值的形式为“yyyy-mm-dd hh:mm:ss',dt1和dt2之间的秒数
unix_timestamp(1358498312) - unix_timestamp(517378271);
unix_timestamp( dt2 ) - unix_timestamp( dt1 )
要得到的分钟数除以60,除以3600,和天数的小时数,除以3600 * 24。
3、显示列的值发生n次
select id
from tbl
group by id
having count(*) = n;
4、计算两个日期之间的数量
最简单的任意两个日期之间的数个工作日的支持是d列日和节假日布尔填充的所有的日子在所有可能相关的年度日历表。那么下面的查询会包容的营业天数日期间启动和停止:
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、是你的数据库有多大
select
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 ;
bitscn.com