943,559 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 1011
  • MySQL RSS
Jan 30th, 2009
0

Some strange logical error in my code

Expand Post »
Hi All,
I am mysql newbie.
Please have alook at this querry.

sql Syntax (Toggle Plain Text)
  1. SELECT b.DATE as DATE, a.tirthankar_name as tirthankar_name, a.kalyanak as kalyanak
  2. FROM table1 a, table2 b
  3. WHERE a.mass = b.mass
  4. AND a.paksh = b.paksh
  5. AND a.tithi = b.tithi
  6. AND b.DATE BETWEEN CURDATE( )
  7. AND (CURDATE( ) +9)
  8. ORDER BY b.DATE
  9. LIMIT 0 , 1000;

I am trying to run this querry but it is giving zero rows as output while I am running it on my table.
It was running fine a few days ago but started bugging around 7-8 days back when curdate and curdate+9 range was in January month it self.

I assume the problem is mainly due to change in month from January to February which is causing this trouble.
Is there any better way to code this.
I want to put a few variables where date is today or 9 days from today (that is for next ten days including today).
The date field has values 2009-01-30 for today in both the tables.

Also it is runs and gives output if I change
sql Syntax (Toggle Plain Text)
  1. "AND b.date BETWEEN CURdate( )
  2. AND (CURdate( ) +9)"
to
sql Syntax (Toggle Plain Text)
  1. "AND b.date BETWEEN CURdate( )
  2. AND (CURdate( ) +75)"
Although does not gives output for next 75 days but it does output for next 5 days.

Its a bit urgent
Last edited by peter_budo; Jan 30th, 2009 at 2:39 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjain is offline Offline
3 posts
since Jan 2009
Jan 30th, 2009
0

Re: Some strange logical error in my code

If you want the where to be contingent upon all the AND statements put them in parens.
eg:
sql Syntax (Toggle Plain Text)
  1. WHERE (
  2. AND a.paksh = b.paksh
  3. AND a.tithi = b.tithi
  4. AND b.DATE BETWEEN CURDATE( )
  5. AND (CURDATE( ) +9)
  6. )
Last edited by peter_budo; Feb 2nd, 2009 at 7:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 31st, 2009
0

Re: Some strange logical error in my code

Thanks for suggestion. But it did not work as well.
I figured out while working that it is working like
MySQL Syntax (Toggle Plain Text)
  1. CURDATE()+70
gives entries corresponding to tomorrow.
MySQL Syntax (Toggle Plain Text)
  1. CURDATE()+71
gives entries for day after tomorrow. similarly
MySQL Syntax (Toggle Plain Text)
  1. CURDATE()+90
gives entries for twenty next days.
It suggest that the code is working with a shift of 69 in addition of Curdate().
Any clues for why this is happening.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjain is offline Offline
3 posts
since Jan 2009
Feb 9th, 2009
0

Re: Some strange logical error in my code

What does 'select curdate();' return?
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Feb 10th, 2009
0

Re: Some strange logical error in my code

sql Syntax (Toggle Plain Text)
  1. AND b.DATE BETWEEN CURDATE( ) AND DATE_ADD(CURDATE(), INTERVAL +9 DAY)
Last edited by almostbob; Feb 10th, 2009 at 7:13 pm. Reason: dammit I cant spell
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is online now Online
2,970 posts
since Jan 2009
Feb 10th, 2009
0

Re: Some strange logical error in my code

No, not showing the definition. Rather, showing the current date. For example, on my desktop computer:
MySQL Syntax (Toggle Plain Text)
  1. mysql> SELECT CURDATE();
  2. +------------+
  3. | CURDATE() |
  4. +------------+
  5. | 2009-02-10 |
  6. +------------+
  7. 1 row IN SET (0.00 sec)

If there's an inexplicable discrepancy in the date, I would first check to be sure the SQL server's date is correct.

Upon finding the server's date is correct, I would RTFM and google until I found references to adding and offsetting dates. I would find that one can't simply add to curdate(). One needs to use, yes, another function:
MySQL Syntax (Toggle Plain Text)
  1. mysql> SELECT DATE_ADD(CURDATE(), INTERVAL -11 DAY);
  2. +---------------------------------------+
  3. | DATE_ADD(CURDATE(), INTERVAL -11 DAY) |
  4. +---------------------------------------+
  5. | 2009-01-30 |
  6. +---------------------------------------+
  7. 1 row IN SET (0.00 sec)

or

MySQL Syntax (Toggle Plain Text)
  1. mysql> SELECT DATE_ADD(CURDATE(), INTERVAL +75 DAY);
  2. +---------------------------------------+
  3. | DATE_ADD(CURDATE(), INTERVAL +75 DAY) |
  4. +---------------------------------------+
  5. | 2009-04-26 |
  6. +---------------------------------------+
  7. 1 row IN SET (0.00 sec)

Once the OP uses the correct functions, the code should begin to work.

Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Feb 11th, 2009
0

Re: Some strange logical error in my code

Click to Expand / Collapse  Quote originally posted by almostbob ...
sql Syntax (Toggle Plain Text)
  1. AND b.DATE BETWEEN CURDATE( ) AND DATE_ADD(CURDATE(), INTERVAL +9 DAY)
This code worked.
Thanks Bob (Almostbob) and all other for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjain is offline Offline
3 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Query for 3 tables
Next Thread in MySQL Forum Timeline: Count from multiple tables and group output by date





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC