This is a discussion on Using IF within the MySQL forums, part of the Database Server Software category; --> Hi, I'm not sure if I'm pursuing the wrong avenue, but here is my sql: SELECT * FROM user_table ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, I'm not sure if I'm pursuing the wrong avenue, but here is my sql: SELECT * FROM user_table WHERE uuname='test' AND IF(CURDATE()>=DATE('2008-08-31')), CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') ) I want either of the statements to be appended to the where statement as long as they conform to the if statement. I get the error: Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '), CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), CURDATE() <= DATE('2' at line 1 Not quite sure whats wrong - please advise. Many thanks A |
| |||
| On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > Hi, > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > SELECT * > FROM user_table > WHERE uuname='test' > AND > IF(CURDATE()>=DATE('2008-08-31')), > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > ) > > I want either of the statements to be appended to the where statement > as long as they conform to the if statement. > > I get the error: > Error Code : 1064 > You have an error in your SQL syntax; check the manual that > corresponds to your MySQL server version for the right syntax to use > near '), > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > CURDATE() <= DATE('2' at line 1 > > Not quite sure whats wrong - please advise. > > Many thanks > > A I think you want: SELECT * FROM user_table WHERE uuname='test' AND (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY) OR (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) But note that both cases could deal with the case where CURDATE() is exactly '2008-12-31', is that what you want? |
| |||
| On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > Hi, > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > SELECT * > > FROM user_table > > WHERE uuname='test' > > AND > > IF(CURDATE()>=DATE('2008-08-31')), > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > ) > > > I want either of the statements to be appended to the where statement > > as long as they conform to the if statement. > > > I get the error: > > Error Code : 1064 > > You have an error in your SQL syntax; check the manual that > > corresponds to your MySQL server version for the right syntax to use > > near '), > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > CURDATE() <= DATE('2' at line 1 > > > Not quite sure whats wrong - please advise. > > > Many thanks > > > A > > I think you want: > SELECT * > FROM user_table > WHERE uuname='test' > AND > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > INTERVAL 365 DAY) > OR > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > But note that both cases could deal with the case where CURDATE() is > exactly '2008-12-31', is that what you want? Hi Captain, Basically I want it to execute one line if its after the 31st August 2008 and a different line if its before. So for example it would read "WHERE uuname='test' AND xxx-chosen line based on IF statement. A |
| |||
| On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > Hi, > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > SELECT * > > > FROM user_table > > > WHERE uuname='test' > > > AND > > > IF(CURDATE()>=DATE('2008-08-31')), > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > ) > > > > I want either of the statements to be appended to the where statement > > > as long as they conform to the if statement. > > > > I get the error: > > > Error Code : 1064 > > > You have an error in your SQL syntax; check the manual that > > > corresponds to your MySQL server version for the right syntax to use > > > near '), > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > CURDATE() <= DATE('2' at line 1 > > > > Not quite sure whats wrong - please advise. > > > > Many thanks > > > > A > > > I think you want: > > SELECT * > > FROM user_table > > WHERE uuname='test' > > AND > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > INTERVAL 365 DAY) > > OR > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > But note that both cases could deal with the case where CURDATE() is > > exactly '2008-12-31', is that what you want? > > Hi Captain, > > Basically I want it to execute one line if its after the 31st August > 2008 and a different line if its before. So for example it would read > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > A But what if it is exactly 31st August 2008? |
| |||
| On 17 Dec, 12:55, Captain Paralytic <paul_laut...@yahoo.com> wrote: > On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > Hi, > > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > > SELECT * > > > > FROM user_table > > > > WHERE uuname='test' > > > > AND > > > > IF(CURDATE()>=DATE('2008-08-31')), > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > > ) > > > > > I want either of the statements to be appended to the where statement > > > > as long as they conform to the if statement. > > > > > I get the error: > > > > Error Code : 1064 > > > > You have an error in your SQL syntax; check the manual that > > > > corresponds to your MySQL server version for the right syntax to use > > > > near '), > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > CURDATE() <= DATE('2' at line 1 > > > > > Not quite sure whats wrong - please advise. > > > > > Many thanks > > > > > A > > > > I think you want: > > > SELECT * > > > FROM user_table > > > WHERE uuname='test' > > > AND > > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > > INTERVAL 365 DAY) > > > OR > > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > > But note that both cases could deal with the case where CURDATE() is > > > exactly '2008-12-31', is that what you want? > > > Hi Captain, > > > Basically I want it to execute one line if its after the 31st August > > 2008 and a different line if its before. So for example it would read > > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > > A > > But what if it is exactly 31st August 2008? Nothing particular - just a random date. Hopefully presented in the correct format. |
| |||
| On 17 Dec, 12:59, UKuser <spiderc...@yahoo.co.uk> wrote: > On 17 Dec, 12:55, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > > Hi, > > > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > > > SELECT * > > > > > FROM user_table > > > > > WHERE uuname='test' > > > > > AND > > > > > IF(CURDATE()>=DATE('2008-08-31')), > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > > > ) > > > > > > I want either of the statements to be appended to the where statement > > > > > as long as they conform to the if statement. > > > > > > I get the error: > > > > > Error Code : 1064 > > > > > You have an error in your SQL syntax; check the manual that > > > > > corresponds to your MySQL server version for the right syntax to use > > > > > near '), > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > CURDATE() <= DATE('2' at line 1 > > > > > > Not quite sure whats wrong - please advise. > > > > > > Many thanks > > > > > > A > > > > > I think you want: > > > > SELECT * > > > > FROM user_table > > > > WHERE uuname='test' > > > > AND > > > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > > > INTERVAL 365 DAY) > > > > OR > > > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > > > But note that both cases could deal with the case where CURDATE() is > > > > exactly '2008-12-31', is that what you want? > > > > Hi Captain, > > > > Basically I want it to execute one line if its after the 31st August > > > 2008 and a different line if its before. So for example it would read > > > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > > > A > > > But what if it is exactly 31st August 2008? > > Nothing particular - just a random date. Hopefully presented in the > correct format. Apologies - mis read ya. Ignore it for now - will look at it. Could just use PHP and put it in the PHP code - just wondered if there was a way to do it in mysql. |
| |||
| On 17 Dec, 12:59, UKuser <spiderc...@yahoo.co.uk> wrote: > On 17 Dec, 12:55, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > > Hi, > > > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > > > SELECT * > > > > > FROM user_table > > > > > WHERE uuname='test' > > > > > AND > > > > > IF(CURDATE()>=DATE('2008-08-31')), > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > > > ) > > > > > > I want either of the statements to be appended to the where statement > > > > > as long as they conform to the if statement. > > > > > > I get the error: > > > > > Error Code : 1064 > > > > > You have an error in your SQL syntax; check the manual that > > > > > corresponds to your MySQL server version for the right syntax to use > > > > > near '), > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > CURDATE() <= DATE('2' at line 1 > > > > > > Not quite sure whats wrong - please advise. > > > > > > Many thanks > > > > > > A > > > > > I think you want: > > > > SELECT * > > > > FROM user_table > > > > WHERE uuname='test' > > > > AND > > > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > > > INTERVAL 365 DAY) > > > > OR > > > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > > > But note that both cases could deal with the case where CURDATE() is > > > > exactly '2008-12-31', is that what you want? > > > > Hi Captain, > > > > Basically I want it to execute one line if its after the 31st August > > > 2008 and a different line if its before. So for example it would read > > > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > > > A > > > But what if it is exactly 31st August 2008? > > Nothing particular - just a random date. Hopefully presented in the > correct format. What are you talking about? You have said that you want a certain special processing if the date is BEFORE a (random) date. You have said that you want slightly different special processing if the date is AFTER a (random) date. So what is supposed to happen if the date is EXACTLY that particular (random) date? |
| |||
| On 17 Dec, 13:01, UKuser <spiderc...@yahoo.co.uk> wrote: > On 17 Dec, 12:59, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > On 17 Dec, 12:55, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > > > Hi, > > > > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > > > > SELECT * > > > > > > FROM user_table > > > > > > WHERE uuname='test' > > > > > > AND > > > > > > IF(CURDATE()>=DATE('2008-08-31')), > > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > > > > ) > > > > > > > I want either of the statements to be appended to the where statement > > > > > > as long as they conform to the if statement. > > > > > > > I get the error: > > > > > > Error Code : 1064 > > > > > > You have an error in your SQL syntax; check the manual that > > > > > > corresponds to your MySQL server version for the right syntax to use > > > > > > near '), > > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > > CURDATE() <= DATE('2' at line 1 > > > > > > > Not quite sure whats wrong - please advise. > > > > > > > Many thanks > > > > > > > A > > > > > > I think you want: > > > > > SELECT * > > > > > FROM user_table > > > > > WHERE uuname='test' > > > > > AND > > > > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > > > > INTERVAL 365 DAY) > > > > > OR > > > > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > > > > But note that both cases could deal with the case where CURDATE() is > > > > > exactly '2008-12-31', is that what you want? > > > > > Hi Captain, > > > > > Basically I want it to execute one line if its after the 31st August > > > > 2008 and a different line if its before. So for example it would read > > > > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > > > > A > > > > But what if it is exactly 31st August 2008? > > > Nothing particular - just a random date. Hopefully presented in the > > correct format. > > Apologies - mis read ya. Ignore it for now - will look at it. Could > just use PHP and put it in the PHP code - just wondered if there was a > way to do it in mysql. Are you being deliberately stupid? I gave you the answer to your question in my first post on the subject thus: SELECT * FROM user_table WHERE uuname='test' AND (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY) OR (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) But I also pointed out that your original statements were faulty. |
| ||||
| On 17 Dec, 13:04, Captain Paralytic <paul_laut...@yahoo.com> wrote: > On 17 Dec, 13:01, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > On 17 Dec, 12:59, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > On 17 Dec, 12:55, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > On 17 Dec, 12:38, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > > On 17 Dec, 12:35, Captain Paralytic <paul_laut...@yahoo.com> wrote: > > > > > > > On 17 Dec, 12:17, UKuser <spiderc...@yahoo.co.uk> wrote: > > > > > > > > Hi, > > > > > > > > I'm not sure if I'm pursuing the wrong avenue, but here is my sql: > > > > > > > > SELECT * > > > > > > > FROM user_table > > > > > > > WHERE uuname='test' > > > > > > > AND > > > > > > > IF(CURDATE()>=DATE('2008-08-31')), > > > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > > > CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31') > > > > > > > ) > > > > > > > > I want either of the statements to be appended to the where statement > > > > > > > as long as they conform to the if statement. > > > > > > > > I get the error: > > > > > > > Error Code : 1064 > > > > > > > You have an error in your SQL syntax; check the manual that > > > > > > > corresponds to your MySQL server version for the right syntax to use > > > > > > > near '), > > > > > > > CURDATE() <= DATE_ADD(start_date, INTERVAL 365 DAY), > > > > > > > CURDATE() <= DATE('2' at line 1 > > > > > > > > Not quite sure whats wrong - please advise. > > > > > > > > Many thanks > > > > > > > > A > > > > > > > I think you want: > > > > > > SELECT * > > > > > > FROM user_table > > > > > > WHERE uuname='test' > > > > > > AND > > > > > > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > > > > > > INTERVAL 365 DAY) > > > > > > OR > > > > > > (CURDATE() <= DATE('2008-12-31') AND start_date >=DATE('2008-08-31')) > > > > > > > But note that both cases could deal with the case where CURDATE() is > > > > > > exactly '2008-12-31', is that what you want? > > > > > > Hi Captain, > > > > > > Basically I want it to execute one line if its after the 31st August > > > > > 2008 and a different line if its before. So for example it would read > > > > > "WHERE uuname='test' AND xxx-chosen line based on IF statement. > > > > > > A > > > > > But what if it is exactly 31st August 2008? > > > > Nothing particular - just a random date. Hopefully presented in the > > > correct format. > > > Apologies - mis read ya. Ignore it for now - will look at it. Could > > just use PHP and put it in the PHP code - just wondered if there was a > > way to do it in mysql. > > Are you being deliberately stupid? > > I gave you the answer to your question in my first post on the subject > thus: > SELECT * > FROM user_table > WHERE uuname='test' > AND > (CURDATE()>=DATE('2008-08-31') AND CURDATE() <= DATE_ADD(start_date, > INTERVAL 365 DAY) > OR > (CURDATE() <= DATE('2008-12-31') AND start_date > > >=DATE('2008-08-31')) > > But I also pointed out that your original statements were faulty. misread your script. Many thanks. |