6条useful SQL
来源:互联网 发布:js日期选择器 编辑:程序博客网 时间:2024/05/13 06:21
1. 计算年数
你想通过生日来计算这个人有几岁了。
1
2
SELECT
DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)),
'%Y'
) + 0;
2. 两个时间的差
取得两个 datetime 值的差。假设 dt1 和 dt2 是 datetime 类型,其格式为 ‘yyyy-mm-dd hh:mm:ss’,那么它们之间所差的秒数为:
1
2
UNIX_TIMESTAMP( dt2 ) - UNIX_TIMESTAMP( dt1 )
除以60就是所差的分钟数,除以3600就是所差的小时数,再除以24就是所差的天数。
3. 显示某一列出现过N次的值
1
2
3
4
5
SELECT
id
FROM
tbl
GROUP
BY
id
HAVING
COUNT
(*) = N;
4. 计算两个日子间的工作日
所谓工作日就是除出周六周日和节假日。
1
2
3
4
5
6
SELECT
COUNT
(*)
FROM
calendar
WHERE
d
BETWEEN
Start
AND
Stop
AND
DAYOFWEEK(d)
NOT
IN
(1,7)
AND
holiday=0;
5. 查找表中的主键
1
2
3
4
5
6
7
8
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. 查看你的数库有多大
1
2
3
4
5
6
7
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 ;
- 6条useful SQL
- db2 useful SQL
- Useful SQL script
- oracle useful sql
- SQL Syntax - Some Useful Keywords
- General Ledger Useful SQL Scripts
- useful
- Useful
- useful
- useful
- some useful select statement of SQL
- 【转】Useful SQL reference 一些有用的SQL
- Some useful techniques in sql injection [个人总结简洁版]
- Summary of Useful Features in SQL Server 2008
- Useful SQL code, often use in Finance Group
- General Ledger Useful SQL Scripts – Oracle Applications 11i
- General Ledger Useful SQL Scripts – Oracle Applications 11i
- Solution for calculate & Output without Declare (Useful for SQL Developer)
- document.forms的用法(ie,ff适用的方法)
- mediawiki解决方案
- 4.18第四章逻辑运算和选择结构程序
- 我的第一篇技术博客
- 鹰与乌鸦
- 6条useful SQL
- eclipse c/c++ unresolved inclusion
- ERP:深化应用从内到外
- Scheduling, Thread Context, and IRQL
- 开博啦。。。
- 口渴的鸽子
- Spring学习总结10(测试)
- 小母牛与公牛
- 在access中支持2个以上left join的方法