Jump to content

Archived

This topic is now archived and is closed to further replies.

azuka

Searching Multiple Tables

Recommended Posts

I have two tables I want to search for content -- One is called auth_books and the other auth_chapters. Their structures are as follows:
auth_books:
BookId => (int)
BookName => (varchar)
Description => (text)

auth_chapters:
ChapId => (int)
ChapName => (varchar)
ChapText => (text)
BookId => (int)

I want to search both tables and return content that contains keywords. What kind of query do I need to write?

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]select * FROM auth_books, auth_chapters WHERE
auth_books.BookName LIKE '$search' OR
auth_books.Description LIKE '$search' OR
auth_chapters.ChapName LIKE '$search' OR
auth_chapters.ChapText LIKE '$search'[/quote]

OR a better way

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]mysql_query("select * FROM auth_books, auth_chapters WHERE '$search' IN (auth_books.BookName, auth_books.Description, auth_chapters.ChapName, auth_chapters"));[/quote]


Just make a var called $search with the search in :)
Should do the job :)

Share this post


Link to post
Share on other sites
The query has no join condition specified.

If you have 50 books with 10 chapters each then you will have 500 rows in the chapters table and, if you join on BookId, (WHERE auth_books.BookId = auth_chapters.BookId) you would be searching 500 rows.

Because no join is specified it will join every book row with every chapter row so you will be searching 25,000 rows. Expect some duplication of the search results.

Share this post


Link to post
Share on other sites
Thanks a lot for all the help -- I'm using full-text search instead. I noticed that if I don't specify 'IN Boolean Mode' I don't get any results -- is it because of the character sets I'm using? Which character set is good for case-insensitive search?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.