本文介绍了如何选择30天之内的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有疑问:
SELECT name
FROM (
SELECT name FROM
Hist_answer
WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)
UNION ALL
SELECT name FROM
Hist_internet
WHERE id_city='34324' AND datetime >= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)
) x
GROUP BY name ORDER BY name
但是 DATE_SUB 是MySQL函数,我需要MsSQL 2008的函数
But DATE_SUB is a MySQL function and I need function for MsSQL 2008
请告诉我如何使用MsSQL 2008从30天中选择数据?
Tell me please how to select data from 30 days by using MsSQL 2008?
P.S.:日期时间的数据类型为smalldatetime
P.S.: Data type of datetime is smalldatetime
推荐答案
您应该使用DATEADD
是Sql server,因此,如果尝试此简单选择,您将看到影响
You should be using DATEADD
is Sql server so if try this simple select you will see the affect
Select DATEADD(Month, -1, getdate())
结果
2013-04-20 14:08:07.177
在您的情况下,请尝试此查询
SELECT name
FROM (
SELECT name FROM
Hist_answer
WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE())
UNION ALL
SELECT name FROM
Hist_internet
WHERE id_city='34324' AND datetime >= DATEADD(month,-1,GETDATE())
) x
GROUP BY name ORDER BY name
这篇关于如何选择30天之内的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!