3

複数のテーブルがあり、列が異なるため、それに応じてフルテキストインデックスが設定されます。スコアは関連性によってデータをソートするため、1つのテーブルのみを検索する場合は問題ありません。ただし、複数のテーブルがあり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

2 つのオプション:

  1. ビューから選択してソートできるビューを作成します。
  2. クエリを from (ネストされた query as table with 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 に答える