Jump to content


Photo

Searching Multiple Tables


  • Please log in to reply
3 replies to this topic

#1 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 25 March 2006 - 04:19 AM

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?

Given enough eyeballs all bugs are shallow. My Website

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 25 March 2006 - 07:36 AM

[!--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 :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 25 March 2006 - 12:20 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 azuka

azuka
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts
  • LocationNigeria

Posted 25 March 2006 - 11:47 PM

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?
Given enough eyeballs all bugs are shallow. My Website




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users