1. 计算年数
你想通过生日来计算这个人有几岁了。
SELECTDATE_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次的值
SELECTid
FROMtbl
GROUPBYid
HAVINGCOUNT(*)=N;
FROMtbl
GROUPBYid
HAVINGCOUNT(*)=N;
4. 计算两个日子间的工作日
所谓工作日就是除出周六周日和节假日。
SELECTCOUNT(*)
FROMcalendar
WHEREdBETWEENStartANDStop
ANDDAYOFWEEK(d)NOTIN(1,7)
ANDholiday=0;
FROMcalendar
WHEREdBETWEENStartANDStop
ANDDAYOFWEEK(d)NOTIN(1,7)
ANDholiday=0;
5. 查找表中的主键
SELECTk.column_name
FROMinformation_schema.table_constraints t
JOINinformation_schema.key_column_usage k
USING (constraint_name,table_schema,table_name)
WHEREt.constraint_type='PRIMARY KEY'
ANDt.table_schema='db'
ANDt.table_name=tbl'
FROMinformation_schema.table_constraints t
JOINinformation_schema.key_column_usage k
USING (constraint_name,table_schema,table_name)
WHEREt.constraint_type='PRIMARY KEY'
ANDt.table_schema='db'
ANDt.table_name=tbl'
6. 查看你的数库有多大
SELECT
table_schemaAS'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)'
FROMinformation_schema.tables
GROUPBYtable_schema ;
table_schemaAS'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)'
FROMinformation_schema.tables
GROUPBYtable_schema ;
发表评论