Jump to content

Searching Multiple Tables


azuka

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?
Link to comment
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 :)
Link to comment
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.
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.