MySQL查询使用UNION - PHP PDO问题

时间:2014-01-03 07:33:10

标签: php mysql pdo

我已经构建了一个几乎可以正常运行的基本搜索引擎。我在查询中使用UNION,以便在两个表blogpages中搜索特定值。我也在使用PDO风格。总共有13行。当我使用在两个表的每一列中找到的关键字blah执行查询时。我只从SELECT COUNT(*) FROM blog获得了5个结果,而SELECT COUNT(*) FROM pages没有任何结果。此外,我正在使用:searchquery => $serachquery的准备语句来存储关键字值,但在回显值时,我得到:searchquery而不是blah。如何显示正确的结果? DEMO

PHP

<?php
include("db_con/db_con.php");
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
  $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
  if($_POST['filter1'] == "All Tables"){
   $sqlCommand = "(SELECT COUNT(*) FROM blog WHERE blog_title LIKE :searchquery OR blog_body LIKE :searchquery) UNION (SELECT COUNT(*) FROM pages WHERE page_title LIKE :searchquery OR page_body LIKE :searchquery) ";
  }
  $sql_prepare = $db_con->prepare($sqlCommand);
  if($sql_prepare->execute( array("searchquery"=>'%'.$searchquery.'%') )){
    $count = $sql_prepare->fetchColumn();
    if($count > 1){
      $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand";
    } else {
      $search_output = "<hr />0 results for <strong>$searchquery</strong>$sqlCommand<hr />";
    }
  }
}
?>

HTML

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For:
  <input name="searchquery" type="text" size="44" maxlength="88">
Within:
<select name="filter1">
<option value="All Tables">All Tables</option>
</select>
<input name="myBtn" type="submit">

3 个答案:

答案 0 :(得分:1)

我做了一个方形的: http://sqlfiddle.com/#!2/82482/3

如您所见,您的查询返回两行 - 这可能是您的问题。 您可以增强查询以对两个结果求和:

SELECT SUM(results)
FROM 
      (SELECT COUNT(*) as results
      FROM blog 
      WHERE blog_title LIKE '%blah%' OR blog_body LIKE '%blah%' 
      UNION
      SELECT COUNT(*) as results
      FROM pages 
      WHERE page_title LIKE '%blah%' OR page_body LIKE '%blah%') a

答案 1 :(得分:1)

首先 UNION 发生* 请参阅下面的注释),在您的情况下为您提供两行结果集。

| COUNT(*) |
|----------|
|        5 | -- with fetchColumn() you read a value of only the first row
|        8 |

但您只获取第一行的值。这就是为什么你总是得到5而不是13

注意:在您的特定情况下UNION的使用无效,因为它只返回不同的值。这意味着如果两个选项恰好具有相同的值(例如5),那么您的结果集将看起来像这样

| COUNT(*) |
|----------|
|        5 | 

而不是

| COUNT(*) |
|----------|
|        5 | 
|        5 |

您可能已经使用UNION ALL而不是UNION,并在结果集上迭代并在php中获得总和,或者使用具有聚合函数{{1}的外部SELECT }。


现在要获得总计数,您只需执行此操作

SUM()

输出:

| TOTAL |
|-------|
|    13 |

这是 SQLFiddle 演示


现在就PHP代码而言

1)此检查

SELECT 
(
  SELECT COUNT(*) 
          FROM blog 
         WHERE blog_title LIKE :searchquery 
            OR blog_body  LIKE :searchquery
) + 
(
  SELECT COUNT(*) 
    FROM pages 
   WHERE page_title LIKE :searchquery
      OR page_body LIKE :searchquery
) total

是不正确的,实际上没用,因为您只能通过搜索条件合法获得一次匹配,并且if($count > 1)... 始终返回值COUNT()或其他正整数。因此,你可以简单地留下唯一的一行

0

2)当您输出查询时,您会看到占位符$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand"; ^^^^^^^^^^^ ,因为您从:search query变量输出原始sql查询。

答案 2 :(得分:1)

现在替换这一行:

$count = $sql_prepare->fetchColumn();

用这个:

$count = 0;
foreach($sql_prepare->fetchall() as $records)
{
    $count += $records[0];
}

它返回所有行的计数;