# Simple MYSQL query help



## DrP (Jul 23, 2005)

Hi,
I want to select everything in a table where the username is not A or B. How do I do the 'or' bit?

SELECT * FROM users WHERE username != 'A' works and doesn't select A
SELECT * FROM users WHERE username != 'A' or 'B' just doesn't select A again
SELECT * FROM users WHERE username != 'A', 'B' gives an error

So, how do I do it? What do I have to use for 'or'?


----------



## brendandonhu (Jul 8, 2002)

I think you want And, not Or. Or would return the whole table since all of the users are either not A or not B.
SELECT * FROM users WHERE username != 'A' AND username != 'B'


----------



## DrP (Jul 23, 2005)

Yep, you're right as usual.
Thanks,


----------



## covert215 (Apr 22, 2006)

In other cases, the 'or' operator is || (the character over the backslash twice)


----------



## DrP (Jul 23, 2005)

So that's what they mean!


----------



## brendandonhu (Jul 8, 2002)

Double pipes and the word OR are interchangeable in MySQL. Be careful with that though as they aren't the same in other databases or in PHP code.


----------



## DrP (Jul 23, 2005)

So what are '||' in php code?


----------



## covert215 (Apr 22, 2006)

the 'or' operator

&& means 'and'
== means 'equals'
!= means 'not equal'


----------



## brendandonhu (Jul 8, 2002)

The double pipes are higher in PHP's order of operations, so || and 'or' can give you different results.


----------



## DrP (Jul 23, 2005)

I'm processing a form and want to make sure several fields are completed. Would the double pipes be applied correctly here?

if (isset($_POST['name']) && $_POST['name'] != "" ||
isset($_POST['date'] && $_POST['date'] != ""||
isset($_POST['email'] && $_POST['email'] != "") {
do this

or would this be better:
if ($_POST['name'] != "" && $_POST['date'] != "" && $_POST['email'] != "") {
do this


----------



## brendandonhu (Jul 8, 2002)

```
if(empty($_POST['name']) || empty($_POST['date'] || empty($_POST['email']))
{
  do this;
}
else
{
  do this;
}
```


----------



## DrP (Jul 23, 2005)

Nice one. Thanks.


----------



## brendandonhu (Jul 8, 2002)

No prob


----------

