我有两张桌子:
staticip有两列:ip,staticipidrm_users有许多列,其中之一是staticipcpe。我希望在staticip表中找到行,即staticipid='2'和表rm_users中的ip不是相等的staticipcpe。
我尝试了下面的sql语句,但是它没有工作。
$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";我有以下信息
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource"发布于 2013-11-16 07:34:45
在其他可能的问题中,您忽略了一个结束括号:
$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)";还有, functions are deprecated。使用MySQLi或PDO。
发布于 2013-11-16 07:35:32
如果你的查询是正确的,你可以使用非IN条款来实现你的目标。
喜欢
Select Id
From Test
Where Id Not In( Select Foo From Bar )见更多信息
发布于 2013-11-16 07:50:04
根据数据库的不同,连接可能比子查询更好(请参见Join vs. sub-query),我还喜欢回退(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';用通俗的英语:
不要忘了考虑索引字段rm_users.staticipcpe、staticip.staticipid (我假设这个字段已经是主键)和staticip.ip,否则您将看到完整的表扫描,而不是利用MySQL的b树从内存中查找。
https://stackoverflow.com/questions/20015999
复制相似问题