Search multiple tables Mysql

I'm working on a advanced search functionality with php and 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.

But if only lets say the blogPost table has cod in its title I would only want to show that and nothing else.

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. 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.

Answers


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.

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.

You'll be able to do this programmatically in your 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.

I'm not sure that this would scale to Amazon.com size, so consult somebody better than me before you get that big.


You will be best served by simplifying your MySQL queries and combining results in your PHP code. 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):

$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.


Need Your Help

<Not Available> and [Thread Destroyed] details in Thread window for Visual Studio 2010

c# .net multithreading visual-studio-2010 callstack

I've been trying to debug some issues related with threads with one application. When I attach to the application I see a window like this one:

SetFocus / Page Cycle - Confusing Error

c# asp.net page-lifecycle setfocus prerender

Can anyone tell me why I would be getting the error in the below trace?