PHP PDO - 从查询中排除特定行

时间:2014-09-23 10:57:43

标签: php mysql pdo

我正在使用PDO连接到我的数据库。我想执行查询并仅从查询中排除特定行。我不知道该怎么做。

根据我从另一个问题得到的建议,我发布这是我目前的解决方案,但它不起作用

$queryProfileCode = $dbh->prepare("SELECT profile_code FROM tableBusinessProfiles WHERE profile_code = ? AND NOT EXISTS(SELECT * FROM tableBusinessProfiles WHERE profile_code = $businessProfileIDUrl)");
$queryProfileCode->bindValue(1,$profileCode);
$queryProfileCode->execute();
$resultTableProfileCode = $queryProfileCode->fetchAll();

我找不到如何从查询中排除特定记录。

2 个答案:

答案 0 :(得分:1)

您的查询应该是

SELECT 
profile_code 
FROM tableBusinessProfiles 
WHERE 
profile_code != $businessProfileIDUrl

SELECT 
profile_code 
FROM tableBusinessProfiles 
WHERE 
id NOT IN ($businessProfileIDUrl);

MySQL "不等于" 操作符!=和<> **

答案 1 :(得分:0)

我不确定你想要达到什么目的?问题是查询?

您想要get ALL profile_codes FROM tableBusinessProfiles WHERE profile_code DOES NOT EQUAL TO $businessProfileIDUrl吗?

然后查询可以是:

SELECT profile_code FROM tableBusinessProfiles WHERE profile_code != $businessProfileIDUrl