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...

Recommended Answers

All 9 Replies

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'";

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...

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

Cheers...

No problem. If your question has been answered, please mark the thread as solved.

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

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

Cool leakbali, thanks! Are you located in Bali?

Cool leakbali, thanks! Are you located in Bali?

you are welcome..

yes, i'm in Nusa Dua, Bali

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.