Jump to content


Photo

where statement


  • Please log in to reply
3 replies to this topic

#1 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 28 July 2006 - 10:20 AM

i have variables in my php code which are set or not set dependent upon the users preferences.

as it is unknown which ones will be set, they all have to include an AND command

so for example these could be some variables

$dog1="AND dog.dog_id IN $users_selection1"
$dog2="AND dog.dog_colour $users_selection2"
$dog3="AND dog.dog_height $users_selection3"

i then have a mysql_query which is as follows (i wont bother with all the php code as its irrelevant, but basically it turns the variables into part of the statement if set by user)
at the moment i put in an sql command which effectively selects all

SELECT pets.petnumber, dog.dog_name
FROM pets
JOIN dog ON pets.dog_id=dog.dog_id
WHERE pets.petnumber is not null $dog1 $dog2 $dog3

So if you looked for dogs that are brown the actual sql command would become

SELECT pets.petnumber, dog.dog_name, cat.cat_name
FROM pets
JOIN dog ON pets.dog_id=dog.dog_id
JOIN cat ON pets.cat_id=cat.cat_id
WHERE pets.petnumber is not null AND dog.dog_colour='brown'


what i want is an alternative way to the "pets.petnumber is not null" example in the above, so in other words a way of skipping straight on to the AND parts of the WHERE statement

any ideas?


#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 July 2006 - 04:16 PM

Huh?  You don't want to have that in your WHERE clause? I'm confused.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 bagnallc

bagnallc
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 28 July 2006 - 05:21 PM

i actually have the answer now.
if i put a 1 in it works fine

sorry if didnt explain well

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 29 July 2006 - 03:13 PM

Oh... I understand... you wanted to be able to add the entire where clause in pieces.  I usually use "1=1" as the base piece.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users