Reputation: 11
I have in php few diferent $id variable.
For example $id=3
, and after that $id=4
, and after $id=5
...`
What I want is to
SELECT from mysql database WHERE id !=$id
I want select id who is different from 3 or 4 or 5
Upvotes: 1
Views: 5426
Reputation: 4148
Try the NOT IN
The NOT operator is used to negate the result of an expression and is of particular use when using the IN operator. For example,lists all the products in our table that do NOT id 3, 4 or 5 simply by used a NOT IN operator combination:
SELECT * from MySQL_database_table WHERE id NOT IN (3,4,5)
Upvotes: -1
Reputation: 1291
Are you trying to get all rows that are not equal to that $id
?
If so, try this:
SELECT * FROM table WHERE id !=$id
EDIT:
But if your $id
is an array with multiple ID's, you will need to do it like this:
$id_list=implode("','",$id);
$sql="SELECT * FROM table WHERE id NOT IN ('$id_list')";
Upvotes: 0
Reputation: 2101
Use quotes and Specify what to SELECT
,
If you have to take all columns, use this:
SELECT *
FROM tableName
WHERE id <> '".$id."'";
OR specify column name(s),
SELECT column_name1, column_name2, column_name3, ...
FROM tableName
WHERE id <> '".$id."'";
Upvotes: -1
Reputation: 3299
If $id
is array then you can use easily following method.
$id=array(2,3,4,9);
$id_list=implode("','",$id);
$sql="SELECT from mysql database WHERE id NOT IN ('$id_list')"; //for better way..
Upvotes: -1