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
https://forums.phpfreaks.com/topic/5745-searching-multiple-tables/
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 :)
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.

Archived

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

×
×
  • 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.