union select 匹配

union select on match against

我有以下代码, 我对在此查询上绑定参数感到困惑,我有 3 个 select 语句并且所有三个语句都使用相同的 $value;

我困惑的部分在这里;

$select->bindValue(':search', $value, PDO::PARAM_STR);
$select->execute([':search' => $value]);

如何将参数绑定到我的所有三个 select 语句,谢谢。

$query = "(SELECT *, 'archive' as flag 
          FROM `archiveArticles` 
          WHERE `stage` = 1 AND 
            MATCH(title,description) 
            AGAINST (:search IN BOOLEAN MODE);) UNION 
            (SELECT *, 'market' as flag 
          FROM `marketNewsArticles` 
          WHERE 
            MATCH(title,description) 
            AGAINST (:search IN BOOLEAN MODE);) UNION 
            (SELECT *, 'business' as flag 
          FROM `businessNewsArticles` 
          WHERE 
            MATCH(title,description) 
            AGAINST (:search IN BOOLEAN MODE);)";

$select = $dbh->prepare($query);                          
$select->bindValue(':search', $value, PDO::PARAM_STR);
$select->execute([':search' => $value]);

简单明确的方法是将它们都称为唯一的东西,那么无论您使用哪个驱动程序或是否将其配置为允许重复占位符都无关紧要。

$query = "  (SELECT *, 'archive' as flag 
            FROM `archiveArticles` 
            WHERE `stage` = 1 AND 
                MATCH(title,description) 
                AGAINST (:search1 IN BOOLEAN MODE)
            ) 
        UNION 
            (SELECT *, 'market' as flag 
            FROM `marketNewsArticles` 
            WHERE 
                MATCH(title,description) 
                AGAINST (:search2 IN BOOLEAN MODE)
            ) 
        UNION 
            (SELECT *, 'business' as flag 
            FROM `businessNewsArticles` 
            WHERE 
                MATCH(title,description) 
                AGAINST (:search3 IN BOOLEAN MODE)
            )";

$select = $dbh->prepare($query);                          
$select->bindValue(':search1', $value, PDO::PARAM_STR);
$select->bindValue(':search2', $value, PDO::PARAM_STR);
$select->bindValue(':search3', $value, PDO::PARAM_STR);
$select->execute();

或者在执行调用中使用参数

$select->execute([':search1' => $value, ':search2' => $value, ':search3' => $value]);

然后删除查询之间的;,它们不是必需的并且PDO不能执行多个查询,它们会使这个查询成为3个单独的查询。