473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to filter and display MySQL information by - This Day, This Week, This Month etc.

ilya Kraft
134 100+
Hello,

I am working on a "Top Ranked" feature for my website. I need to display articles that were top ranked, but I want to show results from This Day, This Week, This Month, All Time and I really have no idea on how to do it. I have a field named creation_date which contains information about time when article was posted. And I somehow need to select information from database where creation_date is equal to This Day, This_Month, This_Week etc.
I assume that I will select information from database like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM articles WHERE creation_date = //What do I put here for different times? (day, week, Month, All time)//
  2.  
Does anyone have an idea on how to achieve this?
Jun 23 '11 #1

✓ answered by Rabbit

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Articles
  3. WHERE ArticleDate = GetDate()
  4. ORDER BY Hits DESC;
  5.  
  6. SELECT *
  7. FROM Articles
  8. WHERE ArticleDate = WeekOfYear(GetDate())
  9.      AND Year(ArticleDate) = Year(GetDate())
  10. ORDER BY Hits DESC;
  11.  
  12. SELECT *
  13. FROM Articles
  14. WHERE ArticleDate = Month(GetDate())
  15.      AND Year(ArticleDate) = Year(GetDate())
  16. ORDER BY Hits DESC;

2 3255
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @StartDate DATETIME, @EndDate DATETIME
  2.  
  3. SELECT
  4.     @StartDate = 
  5.     CASE WHEN 'Today' THEN CAST(FLOOR(CAST(GetDate() AS FLOAT)) AS DATETIME)
  6.     CASE WHEN 'This Week' THEN DATEADD(dd, -1 * (DATEPART(dw, GETDATE()) - 1), CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))
  7.     CASE WHEN 'ThisMonth' THEN DATEADD(dd, -1 * (DATEPART(d, GETDATE()) - 1), CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))
  8.     ELSE '1/1/1900'
  9.     END
  10.  
  11. SELECT
  12.     @EndDate = 
  13.     CASE WHEN 'Today' THEN DATEADD(d, 1, CAST(FLOOR(CAST(GetDate() AS FLOAT)) AS DATETIME))
  14.     CASE WHEN 'This Week' THEN DATEADD(d, 7, DATEADD(dd, -1 * (DATEPART(dw, GETDATE()) - 1), CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)))
  15.     CASE WHEN 'ThisMonth' THEN DATEADD(m,1,DATEADD(dd, -1 * (DATEPART(d, GETDATE()) - 1), CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)))
  16.     ELSE '12/31/9999'
  17.     END
  18.  
  19. SELECT * FROM articles WHERE creation_date >= @StartDate AND creation_date < @EndDate
  20.  
Jun 23 '11 #2
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM Articles
  3. WHERE ArticleDate = GetDate()
  4. ORDER BY Hits DESC;
  5.  
  6. SELECT *
  7. FROM Articles
  8. WHERE ArticleDate = WeekOfYear(GetDate())
  9.      AND Year(ArticleDate) = Year(GetDate())
  10. ORDER BY Hits DESC;
  11.  
  12. SELECT *
  13. FROM Articles
  14. WHERE ArticleDate = Month(GetDate())
  15.      AND Year(ArticleDate) = Year(GetDate())
  16. ORDER BY Hits DESC;
Jun 23 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David | last post by:
I do some website design here and there occasionally for a few clients and I've got a new one that wants a database with their website that will display pricelists displayed for their products and...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
3
by: Bob | last post by:
I wish to group data on a report by week, month and year. Crystal reports has this ability as a built in function. Is there a quick way to do this in Access97/2000 VBA reports ? Thank you in...
3
by: | last post by:
Hi!! Did Infragistics or some other company else provide a web calendar conrol with the features and look of Microsoft Outlook calendar? I mean that feature that you can choose Day / Work Week /...
0
by: Andy Holyer | last post by:
We're producing a commercial intranet application using Mason, and we need to produce a room booking system, which needs displays by day, week, and month at-a-time. I've started on twiddling...
2
by: Stephen | last post by:
Hi, Suppose there is a column in the dataset that is a very large field (say varchar(500)) and i want to display partial information with (....) so that the user can click on it to view for...
0
by: jason | last post by:
I need to construct an application that would display a week's worth of dates at a time, Is there any way to limit the number of CalendarDay objects that the System.Web.UI.WebControls.Calendar...
1
by: sang | last post by:
Hi how to list the inserted dates by week,month and year. That is how to select the dates by past one week. similarly the month and year of the particular filed. Thanks in Advance Sang
1
by: pyssarma | last post by:
hi, I am new to mysql (4.0) php combination. Trying to display mysql table data using php. I have tested connection successfully but could not display table data using php. What could be the...
1
by: mramsay | last post by:
Hi there, I have a dynamic hyperlink Baseball. When the user select the hyperlink they are redirected to another page. After that I want to display information on the page from my linkstbl. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.