0

Hi all,

This has always confused me to no end and now that I really need a JOIN I need to get this.

I am working with a small blogging section of my site here. In one table I have "blogs" and another table I have "comments".

My comments table stores the id of "blogs" as well as the comment, id of the commentor and idea of the blog originator.

I have the id blog stored and included it in my link
I would like to get all the data from both tables related to this blog id which is stored in both tables and loop all data for comment display for that blog id.

I have this now but it doesn't seem to be handling my needs:

$blogView = $_GET['blogView']; //This stores the id of the blog being shown


//Now I want to get all comments and other pertinent data to display comments, commenters and date of comment
$sqlC = "SELECT table_comments.*, table_blogs.*  FROM table_comments INNER JOIN table_blogs ON table_comments.id_blog, table_blogs.id_blog = '$blogView'";
$resultC = mysql_query($sqlC) or die(mysql_error());
$rowC = mysql_fetch_array($resultC);

I've tried reading MySQL's website on JOINS but I am not getting it.

Any help is greatly appreciated...

3
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by leakbali
0

Old:

$sqlC = "
  SELECT *  
  FROM table_comments AS c, table_blogs AS b
  WHERE c.id_blog = b.id_blog
  AND c.id_blog = '$blogView'";

New:

$sqlC = "
  SELECT *
  FROM table_comments AS c
  JOIN table_blogs AS b
    ON c.id_blog = b.id_blog
  WHERE c.id_blog = '$blogView'";

Edited by pritaeas: n/a

0

Really appreciate that pritaes. When I JOIN tables, must I always rename the table with "AS newname"?

If so, why is that?

Thanks again for the solution and appreciate some insight on that...

0

fair enough...thanks for your help. The little things can really slow things down.

Cheers...

0

another quick question...if I am joing 3 tables would I just add JOIN table_whatever again after the last JOIN?

0

another quick question...if I am joing 3 tables would I just add JOIN table_whatever again after the last JOIN?

Yes, if you want to joining 3 tables, you just add JOIN table_whatever.

SELECT * FROM table_A AS a JOIN table_B AS b ON a.id = b.id JOIN table_C AS c ON b.id = c.id
0

Cool leakbali, thanks! Are you located in Bali?

you are welcome..

yes, i'm in Nusa Dua, Bali

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.