Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Need help & ideas on searching multiple tables - full text search or is another way??
Old 02-01-2010, 04:10 PM Need help & ideas on searching multiple tables - full text search or is another way??
Novice Talker

Posts: 7
Name: Qadoshyah Fish
Trades: 0
Hi all,

I'm fairly new to PHP & MySQL, so I need some help!

I have 9 tables joined into my search. At the moment I'm using the code below to return the results.

Code:
WHERE asmnt_parcel.Account LIKE '{$search}' OR asmnt_parcel.OwnersName LIKE '{$search}' OR asmnt_parcel.ParcelID LIKE '{$search}' OR asmnt_legal.Legal LIKE '{$search}'

I have more tables that I need to be able to pull records from in a search though, so obviously the above method won't work too well. It will only let me do 4 tables with the above method and then I can't add in anything else.

So, I'm looking into full text searching.

I can full text search from one table, but when I try to add in a second table to be full text searched from, it won't work.

This query works perfect:


Code:
"SELECT appr_agland.Account, appr_agland.Acres, appr_resident.Account, appr_resident.YearBuilt
           FROM appr_agland
           LEFT JOIN appr_resident
           ON appr_agland.Account=appr_resident.Account
           WHERE MATCH (appr_agland.Account) AGAINST('$search')";

But, as soon as I try to add in the second table, kind of like this query below (note bolded part), it doesn't work:


Code:
"SELECT appr_agland.Account, appr_agland.Acres, appr_resident.Account, appr_resident.YearBuilt
           FROM appr_agland
           LEFT JOIN appr_resident
           ON appr_agland.Account=appr_resident.Account
           WHERE MATCH (appr_agland.Account) AGAINST('$search') OR MATCH (appr_resident.Account) AGAINST('$search')";


How can I make it so that I can full text search from multiple tables? Or, is there a better way to search from multiple tables?

Any & all help, is greatly appreciated! I haven't found the most efficient way to search from multiple tables.

Thanks for any & all help!!
Qadoshyah
CGDesigns is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-02-2010, 03:24 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The problem with an OR condition is that the DB has to evaluate everything.
So, as it must parse the whole tables, it skips the indexes, and goes in a table scan.

As you have noticed, table scan are baaaaaaad.

Try to use an union rather than OR:
Code:
SELECT * 
FROM(
    SELECT appr_agland.Account, appr_agland.Acres, appr_resident.Account, appr_resident.YearBuilt
       FROM appr_agland
       LEFT JOIN appr_resident
       ON appr_agland.Account=appr_resident.Account
       WHERE MATCH (appr_agland.Account) AGAINST('$search')
    UNION
    SELECT appr_agland.Account, appr_agland.Acres, appr_resident.Account, appr_resident.YearBuilt
       FROM appr_agland
       LEFT JOIN appr_resident
       ON appr_agland.Account=appr_resident.Account
       WHERE MATCH (appr_resident.Account) AGAINST('$search')
) AS global
and extend the union with your tables as needed.
Each query in the UNION is evaluated independently, and their results are associated.
For that, each query must have the same number of columns in the select, and they must be of the same types.
You cannot have a query that have a varchar filed in the second position, and a timestamp in another and use a union between the two.

A simple "UNION" merge duplicates. If you want to have them, use a "UNION ALL" keyword.
Just a side node, some DB transform implicitly an LEFT JOIN into a LEFT OUTER JOIN, which can be very costly if the table grows.
I personally likes to specify that I want an INNER JOIN or an LEFT INNER JOIN or a LEFT OUTER JOIN.
At least I'm certain of what the db does.

Now, about the query up there, it's surely is not optimal, but it should be much better than using OR in the WHERE clause.
Give it a try...
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 02-02-2010 at 03:26 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-03-2010, 03:07 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
Novice Talker

Posts: 7
Name: Qadoshyah Fish
Trades: 0
Thank you! I will have to give that a try!

Qadoshyah
CGDesigns is offline
Reply With Quote
View Public Profile
 
Old 02-03-2010, 04:36 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
Novice Talker

Posts: 7
Name: Qadoshyah Fish
Trades: 0
I'm not able to get that code to work at all, for some reason. I tried taking out a few things, making sure everything was written right and nothing will pull up. Not even the search page, it's just a blank page.

Not sure why.

Qadoshyah
CGDesigns is offline
Reply With Quote
View Public Profile
 
Old 02-03-2010, 05:07 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Without an error message, there is not much we can do.
Did you enabled error reporting ?
PHP Code:
error_reporting(E_ALL); 
And error display ?
PHP Code:
ini_set('display_errors',true); 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-04-2010, 02:28 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
Novice Talker

Posts: 7
Name: Qadoshyah Fish
Trades: 0
Yeah, I have the code to show error messages in my script. The page isn't even loading. It's just showing up as a white page. Like something is wrong with the code. I haven't looked at it super close yet to see for sure, but I didn't see anything right off the top.

Qadoshyah
CGDesigns is offline
Reply With Quote
View Public Profile
 
Old 02-04-2010, 05:33 PM Re: Need help & ideas on searching multiple tables - full text search or is another w
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then it means that either you are hiding the error message, or that you reporting level is too low.

Check that please.
Without those informations, nothing I can do.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Need help & ideas on searching multiple tables - full text search or is another way??
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.32484 seconds with 12 queries