954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Full-text search on two tables

Hi, I managed to use the full-text search on a single table (product) and would like to search two tables (product and article) simultaneously, when user enter a keyword. Can anyone help?

Thanks

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

Have been wondering the same. I believe you can only do a full text search on selected fields within a single table. In the past when I was in dire need of this feature and didn't have time to experiment, I did a brute force method where I ran a full text search on one table, a second full text search on a second table, and then merged the resultsets.

Of course, you can do a full text search on the article table and left join in the product table (without searching it).

cscgal
The Queen of DaniWeb
Administrator
19,433 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

em... interesting. I was thinking of storing results from both tables (separate full-text search as you mentioned) in array, then sort them based on their scores, and display them. Not sure this may cause a long delay... any thought?

zippee
Posting Whiz in Training
294 posts since Jan 2005
Reputation Points: 10
Solved Threads: 7
 

Yes, that's what I've done in the past. It did put a heavy load on the server.

cscgal
The Queen of DaniWeb
Administrator
19,433 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 230
 

i hve a problem in searching a two tables..?

mr.adrian
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Sorry to revive such an old thread, but I rather hoped that since newer versions of MySQL are now in wide circulation an answer will be available.
I tried the following:

SELECT pd.products_id, pd.products_name, p.products_price
	FROM products_description pd, products p 
	WHERE MATCH (p.products_model) AGAINST ('+Speaker +Mic Miner' IN BOOLEAN MODE)
	AND p.products_id = pd.products_id 
	AND p.products_status = 1

then I tried:

SELECT pd.products_id, pd.products_name, p.products_price
	FROM products_description pd, products p 
	WHERE MATCH (pd.products_name,pd.products_description) AGAINST ('"Speaker Mic" >Miner' IN BOOLEAN MODE)
	AND p.products_id = pd.products_id 
	AND p.products_status = 1

I get a total of seven results returned from these two queries. However, when I tried the following I got a little over 62K results.

SELECT pd.products_id, pd.products_name, p.products_price
	FROM products_description pd, products p 
	WHERE MATCH (p.products_model) AGAINST ('+Speaker +Mic Miner' IN BOOLEAN MODE)
       OR MATCH (pd.products_name,pd.products_description) AGAINST ('"Speaker Mic" >Miner' IN BOOLEAN MODE)
	AND p.products_id = pd.products_id 
	AND p.products_status = 1

Does anyone have any idea how to accomplish this?

jsalis
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

I found the solution to this. I was apparently very close. The MATCH AGAINST OR MATCH AGAINST was the way to go, but I was not joining the tables correctly. Here is again with LEFT JOIN instead and relevancy scoring.

SELECT pd.products_id, pd.products_name, p.products_price, MATCH (p.products_model) AGAINST ('LAA 0209' IN BOOLEAN MODE)
      OR MATCH (pd.products_name,pd.products_description) AGAINST ('LAA 0209' IN BOOLEAN MODE) AS score
      FROM products_description pd
		LEFT JOIN products p
		ON pd.products_id = p.products_id 
      WHERE MATCH (p.products_model) AGAINST ('LAA 0209' IN BOOLEAN MODE)
      OR MATCH (pd.products_name,pd.products_description) AGAINST ('LAA 0209' IN BOOLEAN MODE)
      AND p.products_status = 1
      ORDER BY score DESC
jsalis
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You