vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   JOINing a count? (https://vborg.vbsupport.ru/showthread.php?t=35980)

N!ck 03-11-2002 02:04 AM

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!

N!ck 03-16-2002 01:49 AM

someone?

Freddie Bingham 03-16-2002 02:52 AM

something like:

SELECT count(*) as total
FROM user, photos
WHERE user.userid=photo.userid
GROUP BY photo.userid

N!ck 03-16-2002 10:03 PM

okay...thanks! :)

N!ck 03-16-2002 10:46 PM

errrr...how can i merge this with a regular query to list all the users?

N!ck 03-17-2002 06:01 AM

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

Freddie Bingham 03-17-2002 08:29 AM

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

N!ck 03-17-2002 01:55 PM

can you show me how to use that in a php script? lol

wooolF[RM] 03-17-2002 02:00 PM

]
Code:

<?
SELECT count(*) as total , user.username
FROM user, photos
WHERE user.userid=photo.userid
GROUP BY photo.userid
?>

Code:

<?
SELECT count(*) as total , user.username
FROM user
LEFT JOIN photos USING (userid)
GROUP BY photo.userid
?>

PS: I'm not 100% sure about it...

N!ck 03-17-2002 02:01 PM

never mind! i see it now!

N!ck 03-17-2002 02:13 PM

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
($offset is set via a query string, or else it equals zero)

and also, i did COUNT(id) because id exists in the userphotos table and not in the user table.

wooolF[RM] 03-17-2002 02:20 PM

]sry... can't help you... :(

Freddie Bingham 03-17-2002 05:19 PM

try

GROUP BY user.userid instead of GROUP BY userphotos.userid

using COUNT(*) is faster than using COUNT(id)

N!ck 03-17-2002 05:54 PM

k thanks again :)

N!ck 03-17-2002 05:56 PM

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)

Freddie Bingham 03-17-2002 08:37 PM

Try this
Code:

SELECT user.userid, user.username, count(userphotos.userid) AS total
FROM user
LEFT JOIN userphoto ON (userphoto.userid=user.userid)
GROUP BY user.userid
ORDER BY user.usergroupid DESC, user.username
LIMIT $offset, 15

This would give you then number of attachments each user has as I verified it. Since the above follows the same logic, it has to work.:
Code:

SELECT user.userid, user.username, count(attachment.userid) as attachmentcount
FROM user
LEFT JOIN attachment ON (user.userid=attachment.userid)
GROUP BY user.userid


N!ck 03-17-2002 08:45 PM

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
  • Page Generation 0.01065 seconds
  • Memory Usage 1,738KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (17)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete