我正在努力获得正确的结果。我想测试两者是否存在。在results
表中,“迈克尔”存在,而“迈克”不存在。
$stmt = $dbnet->prepare("
SELECT * FROM
(SELECT cats AS cats1 FROM results WHERE name = :original) AS a,
(SELECT cats AS cats2 FROM results WHERE name = :parsed) AS b
");
$binding = array(
'original' => 'michael',
'parsed' => 'mike'
);
$stmt->execute($binding);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//if there was a result then output
if($results)
{
echo '<pre>'.print_r($results,1).'</pre>';
}
即使数据库中有“ michael”,我也没有得到任何结果。
如果我测试'original' => 'michael', 'parsed' => 'michael'
,我会得到结果……当然都是一样的,因为我为每个测试了相同的值:
Array
(
[0] => Array
(
[cats1] => 6,11
[cats2] => 6,11
)
)
我期望的是以下之一:
不,我不能使用WHERE name = 'michael' OR name = 'mike'
,因为更改后的操作取决于两者是否都有结果。
答案 0 :(得分:0)
这正按预期进行……即使它们为空也不能总是正确返回结果。
$stmt = $dbnet->prepare("
SELECT
IFNULL( (SELECT cats AS cats1 FROM results WHERE name = :original), '') AS a,
IFNULL( (SELECT cats AS cats2 FROM results WHERE name = :parsed), '') AS b
");