惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

数据库 | 酷 壳 - CoolShell

Cuckoo Filter:设计与实现 | 酷 壳 - CoolShell 性能调优攻略 | 酷 壳 - CoolShell NoSQL 数据建模技术 | 酷 壳 - CoolShell 千万别用MongoDB?真的吗?! | 酷 壳 - CoolShell 图解SQL的Join | 酷 壳 - CoolShell 五个免费开源的数据挖掘软件 | 酷 壳 - CoolShell MySQL性能优化的最佳20+条经验 | 酷 壳 - CoolShell 【原创】SQL栏目树的代码 | 酷 壳 - CoolShell 如何比较两个数据表 | 酷 壳 - CoolShell MySQL: InnoDB 还是 MyISAM? | 酷 壳 - CoolShell
6个有用的MySQL语句 | 酷 壳 - CoolShell
陈皓 · 2010-12-31 · via 数据库 | 酷 壳 - CoolShell

以前本站给大家介绍过《MySQL性能优化的最佳20+条经验》,今天给大家介绍六条比较有用的MySQL的SQL语句,可能很多人都通过PHP来实现这些功能。

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. 查看你的数库有多大


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 ;

希望对你有帮助。

文章:来源

Loading...