![]() |
JOINing a count?
How do I join a count from another table? I want to list all users and to see how many photos (stored in a separate table) a particular user has submitted. Can someone show me the MySQL syntax? Thanks!
|
someone?
|
something like:
SELECT count(*) as total FROM user, photos WHERE user.userid=photo.userid GROUP BY photo.userid |
okay...thanks! :)
|
errrr...how can i merge this with a regular query to list all the users?
|
yeah so it freddie or someone could show me an example of how to list all members and for each one show how many photos they have in a table called "userphotos", i would very much appreciate it :D thanks
|
This would only list those with at least one photo
SELECT count(*) as total , user.username FROM user, photos WHERE user.userid=photo.userid GROUP BY photo.userid or this would show all users, putting 0 for those with none. SELECT count(*) as total , user.username FROM user LEFT JOIN photos USING (userid) GROUP BY photo.userid |
can you show me how to use that in a php script? lol
|
]
Code:
<? Code:
<? |
never mind! i see it now!
|
my problem now is that it only lists MY name!
the query: Code:
SELECT COUNT(id) AS total, user.userid, user.username FROM user LEFT JOIN userphotos USING (userid) GROUP BY userphotos.userid ORDER BY user.usergroupid DESC, user.username LIMIT $offset, 15 and also, i did COUNT(id) because id exists in the userphotos table and not in the user table. |
]sry... can't help you... :(
|
try
GROUP BY user.userid instead of GROUP BY userphotos.userid using COUNT(*) is faster than using COUNT(id) |
k thanks again :)
|
okay, better...but now it outputs "1" instead of "0" (nobody has posted any pics yet, but they all have 1's next their names)
|
Try this
Code:
SELECT user.userid, user.username, count(userphotos.userid) AS total Code:
SELECT user.userid, user.username, count(attachment.userid) as attachmentcount |
yes it appears to work just fine. thanks so much.
|
All times are GMT. The time now is 10:24 AM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|