Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Multi table sql query

Featured Replies

Hi there again guys!

 

I have the following table layout:

 

BOOK - b_id, b_title, ...

 

PERSON - p_id, p_name, ...

 

PERSON_BOOK - id, book_id, person_id, contributor_type_id

 

CONTRIBUTOR_TYPE - id, type

 

 

Now, each book (BOOK) can have several contributors (PERSON), and each person (PERSON) can contribute to several books (BOOK). A person can be an Author of the book, Translator, etc (CONTRIBUTOR_TYPE). These contributor types are sorted in order of importance for a book. For instance the author is always most important and each book should have a "main contributor". PERSON_BOOK links each person to a book giving them a type (a person can be an author of one book and a translator of another)

 

I am having difficulty when it comes to retrieving and sorting the data for each book. At the minute, let's say I can sort by book title, and price no problem. The way I'm doing this is probably wrong and inefficient and is probably why sorting by main contributor is not working for me. What I want to do is sort by main contributor (This is usually the author, but if the author is not listed then it would be the highest rated CONTRIBUTOR_TYPE associated with the book)

 

At the minute my sql query looks like this

 

SELECT * FROM book AS b, person AS p, person_book AS pb
WHERE b.b_id=pb.book_id AND p.p_id=pb.person_id 
ORDER BY b_price DESC, pb.contributor_type_id ASC

 

After running that query I simply loop through each entry. After a book is added the b_id is added to an array, and I simply ignore all other entries if the b_id is already in that array.

 

When I try to sort by main contributor, the main contributor for each book becomes whoever's name starts with the lowest letter. I can't link a main contributor to a book and then sort based on that.

 

Can any one help me?

 

Thanks all

  • Author

Didn't read properly but I believe you're looking to use sql joins?

 

I've been reading up on joins but can't find a suitable solution. Can anybody else help?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.