selecting by date

This is a discussion on selecting by date within the MySQL Database forums, part of the Database Forums category; Suppose I have a month and a year (this is for a blog application): $month = 10; // October $year = 2005; // year ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-28-2005
Peter
 
Posts: n/a
Default selecting by date

Suppose I have a month and a year (this is for a blog application):

$month = 10; // October
$year = 2005; // year 2005

I'd like to select items on or before the last day of October 2005 in
descending order. My first attempt was:

echo "SELECT * FROM $tblPosts " .
"WHERE MONTH(postDate)<=$month & YEAR(postDate)<=$year " .
"ORDER BY postDate DESC, id DESC LIMIT $to_request");

but for some reason, this is selecting ALL records.


Given a month and year, how do I select any item with a postDate (which is a
datetime field) on the last day of the month or earlier in the same year?

Thanks!
Pete
Reply With Quote
  #2 (permalink)  
Old 11-28-2005
Bill Karwin
 
Posts: n/a
Default Re: selecting by date

Peter wrote:
> echo "SELECT * FROM $tblPosts " .
> "WHERE MONTH(postDate)<=$month & YEAR(postDate)<=$year " .


The & symbol is not a boolean AND in SQL.

In MySQL, this is a bitwise AND operator, which may have different
behavior than you expect when applied to boolean terms.

Try the AND operator instead.

Regards,
Bill K.
Reply With Quote
  #3 (permalink)  
Old 11-28-2005
Peter
 
Posts: n/a
Default Re: selecting by date

Bill Karwin <bill@karwin.com> wrote:
> Peter wrote:
>> echo "SELECT * FROM $tblPosts " .
>> "WHERE MONTH(postDate)<=$month & YEAR(postDate)<=$year " .

>
> The & symbol is not a boolean AND in SQL.
>
> In MySQL, this is a bitwise AND operator, which may have different
> behavior than you expect when applied to boolean terms.
>
> Try the AND operator instead.
>
> Regards,
> Bill K.


Oh, good gravy!!! How embarrasing!

Thanks for clearing that up. *sheepish grin*

Pete
Reply With Quote
  #4 (permalink)  
Old 11-29-2005
Jerry Stuckle
 
Posts: n/a
Default Re: selecting by date

Peter wrote:
> Bill Karwin <bill@karwin.com> wrote:
>
>>Peter wrote:
>>
>>>echo "SELECT * FROM $tblPosts " .
>>> "WHERE MONTH(postDate)<=$month & YEAR(postDate)<=$year " .

>>
>>The & symbol is not a boolean AND in SQL.
>>
>>In MySQL, this is a bitwise AND operator, which may have different
>>behavior than you expect when applied to boolean terms.
>>
>>Try the AND operator instead.
>>
>>Regards,
>>Bill K.

>
>
> Oh, good gravy!!! How embarrasing!
>
> Thanks for clearing that up. *sheepish grin*
>
> Pete



Almost:

"SELECT * FROM $tblPosts " .
"WHERE YEAR(postDate) < $year OR " .
"(YEAR(postDate) = $year AND MONTH($postDate) <= $month"

Your existing query won't get November and December of any year.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #5 (permalink)  
Old 11-29-2005
Christian Kirsch
 
Posts: n/a
Default Re: selecting by date

Peter schrieb:
> Suppose I have a month and a year (this is for a blog application):
>
> $month = 10; // October
> $year = 2005; // year 2005
>
> I'd like to select items on or before the last day of October 2005 in
> descending order. My first attempt was:
>
> echo "SELECT * FROM $tblPosts " .
> "WHERE MONTH(postDate)<=$month & YEAR(postDate)<=$year " .
> "ORDER BY postDate DESC, id DESC LIMIT $to_request");
>
> but for some reason, this is selecting ALL records.
>
>
> Given a month and year, how do I select any item with a postDate (which is a
> datetime field) on the last day of the month or earlier in the same year?


Try your query in mysql (the commandline tool) - PHP is irrelevant for
your question.
And brush up your SQL - the "AND" operator is definitely not "&".
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:30 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0