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.lng, W.municipalityId, W.title, W.description
FROM water AS W, specie AS S, waterSpecie AS WS 
WHERE LIKE '%$term%' AND = WS.waterId AND = WS.specieId
OR    W.title LIKE '%$term%' AND = WS.waterId AND = 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
select 'blogpost' as tablename, id, description as searchfield from blogpost
select 'water' as tablename, id, description as searchfield from water
select 'specie' as tablename, id, name as searchfield from specie
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 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

// 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# page-lifecycle setfocus prerender

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