Page 1 of 1

"OR" in SQL Query

Posted: Mon Jan 11, 2010 4:52 pm
by easinewe
I would like to select 2 different variables from my database. I wrote it like this:

$sql = "SELECT * FROM `Catalog` WHERE title = '{$article['alsolike']}' OR '{$article['alsolike2']}' ";

That does not seem to work.

The idea would be that it would output as such:

$sql = "SELECT * FROM `Catalog` WHERE title = 'Germany' OR 'Ireland' ";

How would this be written correctly?

Re: "OR" in SQL Query

Posted: Mon Jan 11, 2010 5:49 pm
by requinix
OR in SQL works just like OR in every other programming language out there.

Code: Select all

condition OR condition OR condition...
"Ireland" is not a condition.

Re: "OR" in SQL Query

Posted: Mon Jan 11, 2010 6:22 pm
by SimpleManWeb
"Ireland" is not a condition
To further elaborate,

Code: Select all

 
     $sql = "SELECT * FROM `Catalog` WHERE title = 'Germany' OR `title` = 'Ireland' ";
 
for more advanced queries, you will probably want to break your conditions up using parenthesis.

For your first query, use this:

Code: Select all

 
     $sql = "SELECT * FROM `Catalog` WHERE title = '".$article['alsolike']."' OR `title` = '".$article['alsolike2']."' ";
 
Hope this helps

Re: "OR" in SQL Query

Posted: Mon Jan 11, 2010 8:32 pm
by easinewe
Thanks, that makes sense.

I suffer from a condition called Ireland. I'm learning to deal with it.