我正在使用PHP + MSSQL的博客Web应用程序。
博客表...
我想用几个月从表中收集数据。
我的查询是。
$first_day= date('Y-m-01', $month); // Getting First day of Month
$last_day = date('Y-m-t', $month); // Getting Last Day of Month
$sql = "SELECT
Pk_blog_id,
Blog_title,
Blog_content,
Created_at
FROM dbo.tb_IEAG_Blogs
WHERE Is_archived = 0
AND Created_at >= CONVERT(datetime,'$first_day')
AND Created_at < CONVERT(datetime,'$last_day')
ORDER BY tb_IEAG_Blogs.Pk_blog_id DESC";
我没有按预期得到结果......
当回显查询时如下...
SELECT
Pk_blog_id,
Blog_title,
Blog_content,
Created_at
FROM dbo.tb_IEAG_Blogs
WHERE Is_archived = 0
AND Created_at >= CONVERT(datetime,'2016-02-01')
AND Created_at < CONVERT(datetime,'2016-02-28')
ORDER BY tb_IEAG_Blogs.Pk_blog_id DESC
这可能是MSSQL中的BETWEEN吗?
...谢谢
答案 0 :(得分:1)
LoginPath = new PathString("/Login/"),
试试这个。由于您要比较日期时间,因此2016-02-01日期时间不等于表中的日期时间。
如果你想选择月份,这里有一个更好的SQL。
SELECT
Pk_blog_id,
Blog_title,
Blog_content,
Created_at
FROM dbo.tb_IEAG_Blogs
WHERE Is_archived = 0
AND cast(Created_at as date) >= '2016-02-01'
AND cast(Created_at as date) <'2016-02-28'
ORDER BY tb_IEAG_Blogs.Pk_blog_id DESC