时间:2021-07-01 10:21:17 帮助过:3人阅读
以前本站给大家介绍过《MySQL性能优化的最佳20+条经验》,今天给大家介绍六条比较有用的MySQL的SQL语句,可能很多人都通过PHP来实现这些功能。
1. 计算年数
你想通过生日来计算这个人有几岁了。
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@dateofbirth)), '%Y') + 0;
2. 两个时间的差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. 查看你的数库有多大
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 ;