简体   繁体   中英

mysql - search results - multiple tables

I want to write a query to search in multiple tables (news, articles, projects, files)

While searching I found this

SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press');

I tested it and it's working, but i failed to extend this to multiple tables.

How to write one query that return me search results for multiple tables ?

This is one way to do it. I'm not sure what Match does, but in the where you can also your Match function

Select Table1.Title, Table2.Post FROM Table1, Table2 WHERE Table1.Title = 'press' AND Table2.Title = 'press'

This query will give you the Title and the Post of the 2 tables that have both have press in it.

One method is to use a union :

SELECT title, post FROM news WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM articles WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM projects WHERE MATCH (title,post) AGAINST ('press')
UNION
SELECT title, post FROM files WHERE MATCH (title,post) AGAINST ('press')

This now essentially becomes one pseudo table with all of the records merging into one dataset.

Have a look on this query , you may use like this -

SELECT *
FROM news t1, articles t2, projects t3, files t4 WHERE MATCH ( t1.title, t1.post)
AGAINST ('press')  or Match(t2.title,t2.post) AGAINST('press')

and set all column you want to search set in MATCH() function.

You could try this, it may helpful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM