Jump to content

complex SQL alternative to a PHP setup?


PC Nerd

Recommended Posts

Hi,

 

Ive got a database that has 2 fields that i want to compare: Year_Start and Year_End.

 

if i have a variable that stores a year, I want to return all teh records where my variable is in the range of Year_Start and Year_End.

 

ie - I cant use the following, and I dont want to have to do it in PHP because of the scale of the database.

SELECt * FROM `catalog` WHERE `Year_Start` < $YEAR .......

 

 

Ive been looking for a way to do it in SQL but i cant find a method to do it.

 

Any suggestions of a SQL statement, or a very fast PHP work around woudl be fantastic ( its over 600 records, likely to grow reasonably rapidly).

 

Thanks in advance.

Link to comment
Share on other sites

eg:

 

Year_Start = 2001

Year_End = 2008

 

$YEAR = 2004

 

 

thats basically saying :

 

where 2001 is greater that 2004 AND 2008 is less than 2004

 

that wouldnt work would it????

 

what im really looking for is an SQL solution similar to:

 

where $YEAR > Year_Start AND $YEAR < Year_End

 

however from my knowledge and resources that doesnt work.

 

Thanks for the speedy reply.

Link to comment
Share on other sites

Data:

    Start      End

 

(1) 1999      2005

(2) 2001      2003

(3) 1995      2008

(4) 2008      2008

 

now if i try teh following values:

 

2001: (1) (2) (3)

2000: (1) (3)

2004: (1) (3)

2008: (3) (4)

 

so what Im trying to do is i have a variable.  i want to return all records where that variable($YEAR) is in between the two given fields. ( Start is always smaller that End, and on occaision they will be the same as shown above....

 

 

 

Thanks for all the effore your going to.

 

 

*** edit: Ill try your example Barand - that sounds about correct. Thanks

Link to comment
Share on other sites

*** YIPPE***

Thanks soo much for all your help.

 

Barands solution was correct and is now working.

 

All my resources simply say that the onlw way to do it is where the database field is inbetween the 2 variables.... which i knew sounded wrong but had no idea about how to adapt to my situation.

 

Thanks also MadTechie - I think we were about to hit the nail on the head but Barand beat us.  Thanks :)

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.