sql语句中的减法 [英] Subtraction in sql statement

查看:504
本文介绍了sql语句中的减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此SQL语句合法吗?

     SELECT  firstName, lastName
     FROM  Presidents 
     WHERE  (endTerm-beginTerm)>4;

我需要找到任期超过4年的总统. 我可以在查询中使用-"吗? 如果没有,什么好方法?

I need to find Presidents which had a term longer than 4 years. can i use '-' in this query? if not, what is the good way?

数据类型为endTermbeginTerm

推荐答案

在beginTerm上加上4年,然后进行比较

Add 4 years onto beginTerm, then compare

SELECT  firstName, lastName
     FROM  Presidents 
     WHERE  endTerm > DATE_ADD(beginTerm, INTERVAL 4 YEAR)

由于期之间的边界是如何处理的,因此,这(包括使用其他RDBMS)通常比减去或计算日期差更安全.

This is generally (including using other RDBMS) safer than subtracting or calculating date differences because of how boundaries between periods are handled.

这篇关于sql语句中的减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆