How to use != at query from sql ?

Ex : i have 2 sql tables, table1 & table2.

table1 have 3 rows = id1, id2, id3
table2 have 4 rows = id1, id2, id3, id4

Now i want to print/delete only id4 from table2. I tried several methods, but i cant. please help me.

so table2 looks like this?

id, someothercolumn, evenanothercolumn 1, x, y 2, x, y 3, x, y 4, x, y

Just do
SELECT * FROM table2 WHERE id = 4
or
DELETE FROM table2 WHERE id = 4

[hr]

If it doesn’t look like this then please add the database schema. Easy way to get it is to open phpmyadmin, select export, select table2 and copy/paste the stuff you get from doing that.

Dear JimL, Thank you for reply.

Your answer is correct, but how to find “id4” is not in table1 ?
[it is problem for me because hundreds of ids/rows in both tables, so i want find only which is not in both tables. like id4 ]

We’re going off of what you used for examples, jim gave you a basic example of how to pull just one thing from your tables, but since you have 2 tables, a simple example would be:

mysql_query(“SELECT * FROM table1, table2 WHERE x <> y”);

That’s assuming that column x is available in both tables. <> is the sql equivalent to !=.

Ah, I see.

This should be what you’re looking for

SELECT table2.id FROM table2 WHERE table2.id NOT IN (SELECT table1.id FROM table1)

http://sqlfiddle.com/#!2/2a6fa/1

hi I am using this code for query : [php]
$SQL = mysql_query("SELECT * FROM table1,table2
WHERE table1.id != table2.id
");
while ($row = mysql_fetch_assoc($SQL)) {
echo $id = $row[‘id’];

echo ‘
’ ;
}
[/php]

but result is blank, is my code correct or not ?

Hi JimL,

Your answer is correct, my problem is solved, thanks a lot. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service