Jump to content

[SOLVED] MySQL Multiple results from a single field to multiple field problem


Recommended Posts

Hello all, I'm very new to PHP and MySQL.  I'm working on a comic book database and for my question I'll describe 2 of the tables:

 

Table: Creators

id [int, primary key] (just the typical id)

name [varchar, foreign key] (this contains the names of comic book creators)

 

Table: Comics

id [int, primary key] (just the typical id)

num (contains issue number for the comic)

writer01 [int, links to Creator.id]

writer02 [int, links to Creator.id, null]

writer03 [int, links to Creator.id, null]

illustrator01 [int, links to Creator.id]

illustrator02 [int, links to Creator.id, null]

illustrator03 [int, links to Creator.id, null]

 

Occasionally there are multiple writers/illustrator on a comic book, so I'd like to be able to display all of that information if available (some issues may have just a 1 writer/1 illustrator).

 

When I tried a WHERE to display just Comics.writer01, it worked fine.  When I tried to create a WHERE to display Comics.writer01 and Comics.writer02 I got a "0 results returned".

 

MySQL Query for only Comics.writer01

SELECT Comics.num, Creators.name

FROM Comics, Creators

WHERE Comics.writer01 = Creators.id

 

MySQL Query for both Comics.writer01 and Comics.writer02

SELECT Comics.num, Creators.name

FROM Comics, Creators

WHERE Comics.writer01 = Creators.id

AND Comics.writer02 = Creators.id

 

As I stated earlier, I'm very new to MySQL (and really databases in general).  I have looked through a couple books trying to fully understand JOINS but just don't fully get it yet.  Sorry if its something simple that most people understand, its just evidently taking me longer than I thought...

I'd read into database normalization.

 

http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html

 

Looks like you have a many-to-many relationship going on here.  A comic can have multiple writers/illustrators and the writers/illustrators could have worked on multiple comics.

 

Also, is there ever going to be a chance when the comics have more than 3 writers or illustrators?

Thanks, I forgot about that possibility.  So when I looked up many-to-many, from what I understood I need a 3rd table to reference the two.

 

So I created:

Creator_Books [i relabeled "Comics" to "Books" for short naming purposes]

bid (int) which links to the Books.id

cid (int) which links to the Creators.id

 

And I'm guessing I now need to remove the Writer01,...,Illustrator01,..., from the Books database, since that info is being fed from the intermediary Creator_Books table, correct? 

 

I'm still having trouble with it, so I think I may have the wrong relationship set up.  In phpMyAdmin, I set up the relationship while in the Creator_Books table, linking them to the correct value.  Should it be the other way around, linking from the Books and Creators table into the Creator_Books table?

 

The Query I tried:

SELECT Books.name, Books.num, Creators.name

FROM Books, Creators

LEFT JOIN Creator_Books ON Creator_Books.cid = Creators.id

 

And while it did return results, it didn't return the right results.  For example, I was trying for it to say:

Creator X worked on Book X and Book Y

Creator Y worked on Book X, and Book Z

It returned that Creator X worked on Book X, Y, Z and Creator Y worked on Book X,Y,Z as well as listing them multiple times for each Book.

 

I'm guessing I have the Join wrong, but I tried it several ways and every time I tried doing "Creator_Books.bid = Books.id" it gave me an error...

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.