简体   繁体   English

mysql-搜索结果-多个表

[英]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 我不确定Match的作用,但是在哪里您也可以使用Match函数

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 : 一种方法是使用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. 并在MATCH()函数中设置要搜索的所有列。

You could try this, it may helpful for you. 您可以尝试一下,这可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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