Reputation: 291
I have two tables:
staticip
has two columns: ip
, staticipid
rm_users
has many columns and one of it is staticipcpe
I would like to find rows in staticip
table that is staticipid='2'
and ip
is not equal staticipcpe
in table rm_users
.
I tried the following sql statement but it didn't work.
$sqls="select s.ip, s.staticipid from staticip s
where s.staticipid='2' and not exists
(select u.staticipcpe from rm_users u
where u.staticipcpe=s.ip";
I have the following message
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource"
Upvotes: 1
Views: 70
Reputation: 9582
Depending on your database a JOIN may be better over a sub-query (see Join vs. sub-query), and I also favor backticks (Importance of backtick around table name in MySQL query):
SELECT `s`.`ip`,`s`.`staticipid`
FROM `staticip` AS `s`
INNER JOIN `rm_users` AS `u`
ON `u`.`staticipcpe`<>`s`.`ip`
WHERE `s`.`staticipid`='2';
In plain English:
Don't forget to consider indexing the fields rm_users.staticipcpe
, staticip.staticipid
(I assume this one is already a PRIMARY KEY), and staticip.ip
, otherwise you're looking at full table scans, rather than taking advantage of MySQL's b-tree lookup from memory.
Upvotes: 1
Reputation: 11853
if you query is right you can use NOT IN Clause to achieve your goal
like
Select Id
From Test
Where Id Not In( Select Foo From Bar )
see for more info
Upvotes: 1
Reputation: 44823
Among other possible problems, you're missing a closing parenthesis:
$sqls="select s.ip, s.staticipid from staticip s
where s.staticipid='2' and not exists
(select u.staticipcpe from rm_users u
where u.staticipcpe=s.ip)";
Also, the mysql_* functions are deprecated. Use MySQLi or PDO.
Upvotes: 1