Go Back   CodingForums.com > :: Server side development > MySQL > Other Databases

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2004, 03:46 AM   PM User | #1
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
SQL SELECT for a Blog

I have a bit of trouble on my hands. I am constructing a blog using PHP and PostGreSQL. I need to be able to call out the articles with the usernames and the number of comments for each article. Now, the articles and username I can do no problem, but the number of comments is making me mad (not really, but I can't think of a better word).

Right now, my SQL looks like:

PHP Code:
SELECT greenblogger_users.username
           
greenblogger_articles.*,
           
count(greenblogger_comments.cid
WHERE greenblogger_articles.uid greenblogger_users.uid 
AND greenblogger_articles.nid greenblogger_comments.nid
The Error I get is: ERROR: Attribute greenblogger_users.username must be GROUPed or used in an aggregate function.

I've tried grouping it, but it doesn't work.

Last edited by Kurashu; 09-17-2004 at 03:51 AM..
Kurashu is offline   Reply With Quote
Old 09-17-2004, 04:16 PM   PM User | #2
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
When you use an aggregate function like count or sum then you also need to add a GROUP BY clause which lists ALL table elements that aren't included in the count or sum aggregate function.
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.

Last edited by Roy Sinclair; 09-17-2004 at 04:19 PM..
Roy Sinclair is offline   Reply With Quote
Old 09-17-2004, 10:18 PM   PM User | #3
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
Code:
SELECT greenblogger_users.username,  
           greenblogger_articles.*, 
           count(greenblogger_comments.cid)  
WHERE greenblogger_articles.uid = greenblogger_users.uid  
AND greenblogger_articles.nid = greenblogger_comments.nid
GROUP BY greenblogger_users.username,
           greenblogger_articles.uid,
           greenblogger_articles.nid, 
           greenblogger_articles.name,
           greenblogger_articles.date,
           greenblogger_articles.article;
Now it produces nothing. o.o What am I doing wrong?
Kurashu is offline   Reply With Quote
Old 09-17-2004, 10:27 PM   PM User | #4
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
It doesn't even give you an error of some sort? Just an empty dataset?

Where is the FROM and how is the JOIN between the tables declared?
__________________
Check out the Forum Search. It's the short path to getting great results from this forum.
Roy Sinclair is offline   Reply With Quote
Old 09-17-2004, 11:01 PM   PM User | #5
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
There aren't any. There don't need to be (to my knowledge)

Code:
SELECT greenblogger_articles.*, greenblogger_users.*;
Means select everything from greenblogger_articles and greenblogger_users.

Edit: >_< I keep forgetting that tab won't tab the data but it select the next form item.

Last edited by Kurashu; 09-17-2004 at 11:05 PM..
Kurashu is offline   Reply With Quote
Old 09-18-2004, 07:07 AM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by Roy Sinclair
Where is the FROM and how is the JOIN between the tables declared?
it's a generic join, defined by the where-clause.

Kurashu,

never use table.* in embedded sql !! always specify the fieldlist.
try
SELECT greenblogger_users.username,
greenblogger_articles.uid,
greenblogger_articles.nid,
greenblogger_articles.name,
greenblogger_articles.date,
greenblogger_articles.article;
count(*) as numrec
FROM (greenblogger_articles INNER JOIN greenblogger_users ON greenblogger_articles.uid = greenblogger_users.uid)
INNER JOIN greenblogger_comments ON greenblogger_articles.nid = greenblogger_comments.nid
GROUP BY greenblogger_users.username,
greenblogger_articles.uid,
greenblogger_articles.nid,
greenblogger_articles.name,
greenblogger_articles.date,
greenblogger_articles.article;

you can then get the number for each group with
$row['numrec']
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 09-21-2004, 04:10 AM   PM User | #7
Kurashu
Regular Coder

 
Join Date: Aug 2004
Location: The US of A
Posts: 767
Thanks: 1
Thanked 0 Times in 0 Posts
Kurashu is an unknown quantity at this point
Thanks, and I'll try to avoid *'s in generic table joins.
Kurashu is offline   Reply With Quote
Old 09-21-2004, 07:58 AM   PM User | #8
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
You're welcome
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:15 AM.


Advertisement
Log in to turn off these ads.