Jump to content

CURDATE()


The Little Guy

Recommended Posts

Im adding a part "Todays Hot Products"

 

This will display the day's Hot Products, or the most commonly viewed products for the day.

 

In the viewed_products table, I have a date column, that has a date formatted like: 2007-02-28 10:33:26

I want to get this portion: 2007-02-28 and then it doesn't matter what comes after that.

 

This is the line that Does that

WHERE VIEWED_PRODUCTS.DATE = '(CURDATE()%,INTERVAL 1 DAY)'

Is it correct?

 

Here is the full SQL

SELECT *,COUNT(PRODUCT)
FROM VIEWED_PRODUCTS
INNER JOIN IMAGES
INNER JOIN PRODUCTS 
ON
PRODUCTS.PRODUCT_NUM = VIEWED_PRODUCTS.PRODUCT
WHERE VIEWED_PRODUCTS.DATE = '(CURDATE()%,INTERVAL 1 DAY)'
GROUP BY VIEWED_PRODUCTS.DATE LIMIT 3

Link to comment
Share on other sites

I think you're missing a "DATE_SUB" on there.  I assume the "%" is meant to do some kind of wildcard match against the time portion.  That isn't what is needed. 

 

There are 2 ways to do something like this, depending on what you're after.  You can get all rows with a date equal to yesterday:

DATE(viewed_products.date) = DATE_SUB(CURDATE(),INTERVAL 1 DAY)

 

Or you could get all rows from within the last 24 hours:

viewed_products.date > DATE_SUB(NOW(),INTERVAL 1 DAY)

 

Check out the functions I used here on the mysql docs: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Link to comment
Share on other sites

I can understand that... makes the query more difficult though.  Is there a better way to accomplish the "datetimes from yesterday" than this? -->

viewed_products.date BETWEEN ((CURDATE()+" 0:00:00") - INTERVAL 1 DAY) AND ((CURDATE()+" 23:59:59") - INTERVAL 1 DAY)

Link to comment
Share on other sites

Well if you don't know what you're looking for...

And did fenway slap you yet for using date as a field name? *slap*

 

Now that that is out of the way...

If you want to get products viewed today use:

WHERE DATE(VIEWED_PRODUCTS.DATE) = CURDATE()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.