3

我有不止一个表,有不同的列,全文索引是相应设置的。如果我只想搜索一个表,那没有问题,因为分数会按相关性对数据进行排序。但是我有多个表,我UNION用于这些 sqlSELECT语句如下:

$this->dbi->prepare("
    SELECT `id`,'".PRE."pages' as `table`, MATCH(`title`,`content`) AGAINST (?) AS `score` FROM `".PRE."pages` WHERE MATCH(`title`,`content`) AGAINST (?)
    UNION SELECT `id`,'".PRE."news' as `table`, MATCH(`title`,`content`) AGAINST (?) AS `score` FROM `".PRE."news` WHERE MATCH(`title`,`content`) AGAINST (?)
    UNION SELECT `id`,'".PRE."comments' as `table`, MATCH(`title`, `content`) AGAINST (?) AS `score` FROM `".PRE."comments` WHERE MATCH(`title`, `content`) AGAINST(?)
    UNION SELECT `id`,'".PRE."auction_auto' as `table`, MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST (?) AS `score` FROM `".PRE."auction_auto` WHERE MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST (?)
;")->...

我怎么能与这么多表有相关性?现在无论分数数据都将按照我选择的表格的顺序显示。

谢谢。

有关作者本人的可能解决方案,请参阅此链接

4

1 回答 1

0

两种选择:

  1. 创建一个视图,然后您可以通过从视图中进行选择来进行排序。
  2. 将您的查询放入 from (嵌套查询作为带有 as 的表),然后按顺序排列。见:http ://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html

这是我自己的数据的示例(仅用于示例目的):

select * from
(
  (select * from products where products_id >10)
  UNION
  (select * from products where products_id <= 10)
)
as SOURCE
order by products_id

因此,对于您自己的示例,它将类似于:

    $this->dbi->prepare("
select * from 
(
        SELECT `id`,'".PRE."pages' as `table`, MATCH(`title`,`content`) AGAINST (?) AS `score` FROM `".PRE."pages` WHERE MATCH(`title`,`content`) AGAINST (?)
        UNION SELECT `id`,'".PRE."news' as `table`, MATCH(`title`,`content`) AGAINST (?) AS `score` FROM `".PRE."news` WHERE MATCH(`title`,`content`) AGAINST (?)
        UNION SELECT `id`,'".PRE."comments' as `table`, MATCH(`title`, `content`) AGAINST (?) AS `score` FROM `".PRE."comments` WHERE MATCH(`title`, `content`) AGAINST(?)
        UNION SELECT `id`,'".PRE."auction_auto' as `table`, MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST (?) AS `score` FROM `".PRE."auction_auto` WHERE MATCH(`manufacturer`,`model`,`location`,`other`,`contact`) AGAINST (?)
) as allTables order by `id`;")
于 2013-04-16T20:50:56.250 回答