Re: SQL question!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



What you wrote will give you the list of users who also happen to have comments. What you really want is more like this:

SELECT Users.ID, Users.UserName, Comments.Description
FROM Users LEFT JOIN Comments
ON Users.ID = Comments.UserID;

(note that you'll need to have the UserID foreign key in your Comments table. Also, the "LEFT JOIN" phrase tells whatever sql back-end you're using that you want -all- records from the users table, regardless of whether or not they have a comment-- and any comments per user in the comments table. If you want to eliminate users who don't have comments, use an INNER JOIN instead.

-Mike Forbes

Bruno Braga wrote:

Hey.



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 )



* etc.





thanks in advanced.

Bruno



-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux