Re: SQL question!

From: Date: Tue, 04 May 2004 02:15:03 +0000
Subject: Re: SQL question!
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-34261@lists.php.net to get a copy of this message
I have these tables. Users ( id,name,etc ) Coments : ( id , comment ) How do I do this kind of query: I thought in one thing like this but I cant figure it out. Example: Select * from users order by id desc in (select count (id) from comments) Expected result: List of users: * User1 See comments ( 32 comment in database ) * User2 See coments (13 comments in database )
You need to add a user_id column to comments, which records what user entered each comment. Then you can do SELECT u.name, COUNT(c.id) FROM users u, comments c WHERE u.id=c.user_id GROUP BY (c.user_id) The syntax may not be exactly right but that's the basic idea. Adding a user ID foreign key to the comments table is the important part. Hope that helps, Larry

« previous php.db (#34261) next »