Jump to content


Photo

3 tables, 1 query


  • Please log in to reply
7 replies to this topic

#1 jabbamonkey

jabbamonkey
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 August 2003 - 12:14 PM

I have three tables I\'m trying to combine. Two tables have information regarding a subject, and the third table is what I call the \"Connection\" table. I have the following query....
SELECT   a.comment_id,   a.name,   a.email,   a.comment,   a.live,   b.article_name,  b.article_id,  c.article_id,   c.comment_id,   c.commentjuke_id FROM   comments as a,   articles as b,   connect_commentarticles as c WHERE   c.article_id=\'$id\'   AND   a.comment_id=c.comment_id   AND   a.live=\'1\'";
Basically, Comments for articles are stored in one table. Articles content is stored in the other, and the third table pulls the two tables together (only three columns, for the article id, the comments id, and it\'s own id). I just want to pull the tables together but keep getting an error.

NOTE: the variable \"$id\" is the article id, that will be submitted by the user.

Please let me know if you can help!

#2 michael yare

michael yare
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationLondon, UK

Posted 01 August 2003 - 12:26 PM

You have only joined a to c.

You need to join b to a or c.

#3 jabbamonkey

jabbamonkey
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 August 2003 - 12:29 PM

Would this be right?

SELECT   a.comment_id,   a.name,   a.email,   a.comment,   a.live,   b.article_name,   b.article_id,   c.article_id,   c.comment_id,   c.commentjuke_id FROM   comments as a,   articles as b,   connect_commentarticles as c WHERE   b.article_id=\'$id\'   AND   c.article_id=b.article_id  AND   a.comment_id=c.comment_id   AND   a.live=\'1\'";


#4 michael yare

michael yare
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationLondon, UK

Posted 01 August 2003 - 12:33 PM

Looks good. You only need 1 articleID in the SELECT clause BTW (not that it matters, but its efficient syntax).

#5 jabbamonkey

jabbamonkey
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 August 2003 - 12:43 PM

I keep getting an error ...

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in...


Does it matter the order of the WHERE clause? For example, does it matter if \"b.article_id=c.article_id\" or \"c.article_id=b.article_id\"?


SELECT   a.comment_id,   a.name,   a.email,   a.comment,   a.live,   b.article_name,   c.article_id,   c.comment_id,   c.commentjuke_id FROM   comments as a,   articles as b,   connect_commentarticles as c WHERE   b.article_id=\'$id\'   AND   c.article_id=b.article_id   AND   a.comment_id=c.comment_id   AND   a.live=\'1\'";


#6 michael yare

michael yare
  • Members
  • PipPip
  • Member
  • 25 posts
  • LocationLondon, UK

Posted 01 August 2003 - 03:36 PM

No, the order of the WHERE clauses does not matter.

What are you passing to mysql_num_rows?

I just tested your syntax with one of my a-b-c tables and it worked.

#7 jabbamonkey

jabbamonkey
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 August 2003 - 03:41 PM

After I set my query; here\'s what I have....

$result2 = mysql_query($query1,$db);$num_results = mysql_num_rows($result2);if($num_results!=0) {     do {          // PRINT INFORMATION FROM DATABASE          } while ($myrow2 = mysql_fetch_array($result2)); }


#8 Barand

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

Posted 04 August 2003 - 06:46 PM

Best way is to

echo \"$query1\"

to see what is actually being executed. Could be that $id is empty.
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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users