Jump to content

Query Help


jimmygle

Recommended Posts

I am trying to populate an HTML table with information from multiple db tables, and when I add multiple AND clauses to the statement, I discover that it's pulling one row multiple times based on the AND clauses referring to other tables.  I basically know that it's just pulling it multiple times because it's meeting the criteria multiple times, I think.

 

I want to know if there's anyway in the syntax to tell it to limit certain AND clauses, or is there something else that can do it?  Here's the query, it's for a TV Show site I'm working on.  I think it's adding multiple entries because of the category part of the query, but I'm not sure.

 

mysql_query("SELECT s.id, s.show_name, c.cat_name FROM tv_shows s, tv_episodes e, tv_cats c WHERE s.id=e.show_id AND s.cat_id=c.id ORDER BY show_name ASC");

Link to comment
Share on other sites

I would recommend writing out you queries like this:

 

$select = "SELECT s.id, s.show_name, c.cat_name

              FROM tv_shows AS s

              INNER JOIN tv_episodes AS e ON s.id=e.show_id

              INNER JOIN tv_cats AS c ON s.cat_id=c.id

              ORDER BY s.show_name ASC";

$result = mysql_query($select);

if (mysql_num_rows($results){

  while($row = mysql_fetch_row($result)){

     

  }

}

 

I used INNER JOINs but if the tables under the FROM may not have a record associated to the SELECT you may have to use a LEFT JOIN.  If were you I would read up on JOINs because you will use them a lot. :)  Good luck and have fun.

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.