Jump to content

Cannot make correct query


dannyluked

Recommended Posts

I have these tables:

Blogcomments

-blogtitle

-comment

 

Blog

-title

-date

-other columns...

 

I want a code that shows the ‘title’ column in the table ‘blog’ but also tells you how many rows contain the same title (column ‘blogtitle’)  in the table ‘blogcomments’.

 

For example if my tables looked like this

Blogtitle

A

B

C

C

 

Blog

A

B

C

D

 

The output would be:

A  -  1 match(‘s)

B  -  1 match(‘s)

C  -  2 match(‘s)

D  -  0 match(‘s)

 

MySQL version 5.1

Link to comment
Share on other sites

I just changed the table names and got this:

 

SELECT blogcomments.blogtitle, blogcomments.comment,

COUNT(blog.title)

FROM blog INNER JOIN blogcomments ON (blog.title=blogcomments.blogtitle)

GROUP BY blogcomments.blogtitle

 

but that just give printed "Resource id #10"!

Link to comment
Share on other sites

 

For example if my tables looked like this

Blogtitle

A

B

C

C

 

Blog

A

B

C

D

 

The output would be:

A  -  1 match(‘s)

B  -  1 match(‘s)

C  -  2 match(‘s)

D  -  0 match(‘s)

 

 

If you want the results on this way, the sql that you made is wrong. You got the idea..

 

Try this:

 


SELECT blog.title, COUNT(blogcomments.blogtitle)
FROM blog INNER JOIN blogcomments ON (blog.title=blogcomments.blogtitle)
GROUP BY blogcomments.blogtitle

Link to comment
Share on other sites

Could you please give me the full php code including the connect because when I do it, it shows the title that are in both tables where I want it to count them.

So could you please give me the full code that will do this:

Blogtitle

A

B

C

C

 

Blog

A

B

C

D

 

The output would be:

A  -  1 match(‘s)

B  -  1 match(‘s)

C  -  2 match(‘s)

D  -  0 match(‘s)

 

Thank you very much

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.