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
https://forums.phpfreaks.com/topic/167997-cannot-make-correct-query/
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"!

 

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

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

Archived

This topic is now archived and is closed to further replies.

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