Skip to content
Advertisement

MySQL Query Text Across Multiple Tables

I have two MySQL Tables; City and Country, I want to allow a user to search across the two tables for something which matches in either, and I want a quality match/relevancy order from both tables.

I get a combined result across the two tables, but I get all the city results, then all the country results – I want to get it so the results are mixed and sorted by accuracy… not accuracy of city, then accuracy of country – or is that desirable?

Any other ways to accomplish a query across two tables would be especially welcome, ideally I might need to join a table onto city and a table onto country to add some meta information to the data returned.

Or is there a better way to call the two endpoints and have a relevancy match column, and then merge both endpoints into a json array server side ordered by this relevancy?

Advertisement

Answer

You can wrap the UNION into a subquery and then do the ordering again:

It would be more efficient to create a ranking column:

This will give you 48 results, 24 each from city and country. If you just want the top-ranked results, regardless of which table they came from, just remove the ORDER BY and LIMIT clauses from the UNION:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement