简体   繁体   English

搜索多个表Mysql

[英]Search multiple tables Mysql

I'm working on a advanced search functionality with php and mysql. 我正在使用php和mysql开发高级搜索功能。

The tables are: 这些表是:

blogPost[id, title, description]

water[id, title, description,]

waterSpecie[id, waterId, specieId]

specie[id, name]

user[id, username]

What I want done is this: The user will be able to search from a text field with a keyword, and the results should only be unique once. 我想要做的是:用户将能够从带有关键字的文本字段中进行搜索,并且结果只能是唯一的一次。 For example: 例如:

The user searches for cod then I would like to show all the blogposts that has cod in there titles or description and all the waters that has cods in it and also if a user is nicked cod. 用户搜索鳕鱼,然后我要显示所有在其中包含鳕鱼的博客文章标题或描述,以及其中所有含有鳕鱼的水域,以及用户是否有鳕鱼的昵称。 But if let's say the blogPost has cod in its title and description I don't want it to show up twice in the results list. 但是,如果说blogPost的标题和描述中包含了鳕鱼,我不希望它在结果列表中出现两次。

But if only lets say the blogPost table has cod in its title I would only want to show that and nothing else. 但是,如果只说blogPost表的标题中有cod,我只想表明这一点,别无其他。

I've managed to do it with this sql query: I would now like to add the blogPost table to that query and make it possible to search from it. 我已经设法通过此sql查询来做到这一点:现在,我想将blogPost表添加到该查询中,并可以从中进行搜索。 So if only one table have cod I want to show only the result from that table. 因此,如果只有一个表有鳕鱼,我只想显示该表的结果。

SELECT DISTINCT W.lat, W.lng, W.municipalityId, W.title, W.description
FROM water AS W, specie AS S, waterSpecie AS WS 
WHERE S.name LIKE '%$term%' AND W.id = WS.waterId AND S.id = WS.specieId
OR    W.title LIKE '%$term%' AND W.id = WS.waterId AND S.id = WS.specieId;

I hope I've managed to explain my problem and hopefully I could get some help on this issue. 我希望我已经设法解释了我的问题,希望我能在这个问题上得到一些帮助。

You might try this: 您可以尝试以下方法:

    Select distinct tablename,id from
(select 'blogpost', as tablename, id, title as searchfield from blogpost
union
select 'blogpost' as tablename, id, description as searchfield from blogpost
union
select 'water' as tablename, id, description as searchfield from water
union
select 'specie' as tablename, id, name as searchfield from specie
union
select 'user' as tablename, id, username as searchfield from user) searcher
where searcher.searchfield='cod'

I don't exactly get what's going on with waterSpecie, so I've left it out. 我不完全了解waterSpecie的情况,因此我将其省略。

You should get results something like: 您应该得到如下结果:

tablename   id
blogpost     5
user        12

And then you can query the database to get the record from blogpost with id=5, and the record from user with id=12. 然后,您可以查询数据库以从ID = 5的博客文章获取记录,并从ID = 12的用户获取记录。

You'll be able to do this programmatically in your PHP. 您将可以在PHP中以编程方式执行此操作。 And if you add a table that you want to add to the search functionality, you only have to do it by adding a UNION in the query below, rather than making a join and adding another piece to a WHERE clause. 而且,如果添加了要添加到搜索功能中的表,则只需在下面的查询中添加UNION即可,而不必进行联接并将其他部分添加到WHERE子句中。

I'm not sure that this would scale to Amazon.com size, so consult somebody better than me before you get that big. 我不确定这是否可以扩展到Amazon.com的规模,因此在您做大之前请先咨询我一个人。

You will be best served by simplifying your MySQL queries and combining results in your PHP code. 简化MySQL查询并将结果合并到PHP代码中,将为您提供最好的服务。 The main reason for this in your case is that you want to combine dissimilar results - blog entries if they have the search term and/or waters and other things if they have the search terms. 在您的情况下,这样做的主要原因是您希望合并不同的结果-如果博客条目包含搜索词和/或水域,则合并其他条目,如果它们包含搜索词。

In pseudo code, I'd do something like this (use a DAO in the real code, of course): 在伪代码中,我会做这样的事情(当然,在真实代码中使用DAO):

$foundposts = findblogposts($searchterm);
$foundusers = findusers($searchterm);
$foundwaters = findwaters($searchterm);
// etc

if($foundposts){
    print($foundposts);
}
if($founduers){
    print($foundusers);
}
if($foundwaters){
    print($foundwaters);
}
// etc

That way, you can handle the different formatting requirements you probably have to show blog posts correctly, and users correctly, etc. 这样,您可以处理可能必须正确显示博客文章以及正确显示用户等不同的格式要求。

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

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