MySQL Union 没有正确返回表名

Posted

技术标签:

【中文标题】MySQL Union 没有正确返回表名【英文标题】:MySQL Union don't return table name correctly 【发布时间】:2013-07-28 20:35:24 【问题描述】:

我必须在多个表中搜索:

$query ="(SELECT *, 'product' AS type FROM products WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION 
(SELECT *, 'article' AS type, NULL AS image1, NULL AS image2, NULL AS image3, NULL AS image4 FROM articles WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION 
(SELECT *, 'news' AS type, NULL AS image1, NULL AS image2, NULL AS image3, NULL AS image4 FROM news WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION 
(SELECT *, 'page' AS type, NULL AS categoryID, NULL AS categoryName, NULL AS full_description, NULL AS image, NULL AS image1, NULL AS image2, NULL AS image3, NULL AS image4, NULL AS price FROM pages WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION 
(SELECT *, 'gallery_image' AS type, NULL AS categoryID, NULL AS categoryName, NULL AS full_description, NULL AS image1, NULL AS image2, NULL AS image3, NULL AS image4, NULL AS price FROM gallery_images WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?)";
$selectFromTables = $this->db->prepare($query);
$selectFromTables->execute(array("%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%", "%" . $keyword . "%"));
$found = $selectFromTables->rowCount();
while($rowSearch = $selectFromTables->fetch(PDO::FETCH_ASSOC))

    $id = $rowSearch['id'];
    $type = $rowSearch['type'];
    $title = $rowSearch['title'];
    echo $id . "->" . $type . "<br />";

为什么while循环会返回这个输出?

23->product
24->product
25->product
27->product
28->product
1->
1->
2->
4->
5->

但我希望这个输出:

23->product
24->product
25->product
27->product
28->product
1->article
1->news
2->page
4->page
5->page

【问题讨论】:

可能duplicate 不,它不是重复的。 阅读答案,它将解决您的问题 @davidstrachan 我在这里看不到命名参数。 【参考方案1】:

你很有可能是misusing the UNION statement。

第一个 SELECT 语句中的列名用作返回结果的列名。

我敢打赌,您所有的表都没有相同数量的列(请向我们展示所有相关表的完整描述),从而使您的列别名毫无用处。同样,您的 title 列可能不在所有表中的相同位置。

【讨论】:

是的,我的表的列数不同,我通过添加 NULL 列使它们的列数相同 为时已晚 :) 您的 * 表示“所有列”,因此当您添加 NULL 列时差异已经存在——别名 type 列不一样位置。尝试显式枚举列(所有表的顺序相同)而不是* 如果您不需要应用程序层中的所有列,那么最好不要从数据库中提取所有数据。它通常允许更快地执行查询,更不用说返回给客户端的数据量更少了。 在这种情况下,我需要所有这些 :) 谢谢你的这一点 :)

以上是关于MySQL Union 没有正确返回表名的主要内容,如果未能解决你的问题,请参考以下文章

MySQL - 使用 UNION ALL 获取错误的表字段但结果是正确的

UNION 和 ORDER BY 的使用不正确?

mysql 远程登录与表名大小写问题

MySQL 查看执行计划

mysql没有返回正确的json

MYSQL SUM() 返回不正确的值。没有连接