Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:23:02+00:00 2026-05-22T19:23:02+00:00

I am searching through 3 different tables with fulltext search and I need to

  • 0

I am searching through 3 different tables with fulltext search and I need to detect which table the result is coming from. This is what I have now:

SELECT id, title AS page_title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM pages WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT id, title AS agenda_title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM agenda WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
SELECT id, title AS news_title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score FROM news WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) ORDER BY score ASC

I’m detecting if the page_title has been set and if not if the agenda title has been set etc.
But the result is always from the first query (pages). So if a visitor would search and find a news item, it would display as a page in this case.

So my question is; What am I doing wrong? And is this the best way to do it?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:23:03+00:00Added an answer on May 22, 2026 at 7:23 pm

    When you are using UNIONs like that, add a determinant column, just a constant, to each table, so that you know what table each entry comes from.

    SELECT source, id, title, score
        FROM
        (SELECT 'pages' AS source, id, title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score
            FROM pages WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE)
        UNION
        SELECT 'agenda' AS source, id, title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score 
            FROM agenda WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) UNION
        SELECT 'news' AS source, id, title, MATCH(title) AGAINST(:search_term IN BOOLEAN MODE) AS score 
            FROM news WHERE MATCH(title) AGAINST(:search_term IN BOOLEAN MODE)
    ) ORDER BY score ASC
    

    You will also need to wrap it in a SELECT to ORDER BY over the whole combined set, the way you had it would only order on the last table I think. Changing the title attribute is not a good idea either, as the column names need to be the same for a UNION or it will merge them with the first encountered title, or just blank the column name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried searching through search engines,MSDN,etc. but can't anything. Sorry if this has been
I have been searching through different forums for information and I have tried different
I'm in need of an AJAX solution for searching / filtering through one table
Each of of these threads is searching through a different list of data objects
After searching through some existing libraries for JSON, I have finally ended up with
I went through lots of searching the web which shows how to draw custom
I have been searching for the past through days, trying to figure out how
I have a web page which loads in two different ways; user can click
I had previously asked a question MYSQL Searching multiple tables with different columns using
I am searching a simple method through which i can bind my DataGridColumn with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.