blek
blek

Reputation: 11

Select from mysql where id is different from $id

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

Answers (5)

Ankur Bhadania
Ankur Bhadania

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

Jason Bassett
Jason Bassett

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

Ataboy Josef
Ataboy Josef

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

Ahosan Karim Asik
Ahosan Karim Asik

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

juergen d
juergen d

Reputation: 204746

SELECT * from your_table 
WHERE id not in (3,4,5)

Upvotes: 5

Related Questions