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

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 02-05-2007, 11:45 AM   PM User | #1
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
multiple count & group by?

hi,
I have 4 tables with many records to one table.
eg:

user_schemas
==========
schema_id
user_id
description

servicelines
==========
serviceline_id
schema_id
name

processes
=========
process_id
schema_id
name

I have got the following SQL working counting the number of servicelines for the one schema record. However, I am not sure how to count other stuff. I need one DB query to get the name of the "user_schema" and a count of all the records in the other tables.

here's what I have

Code:
SELECT
us.description,
us.user_id,
count(sl.ud_serviceline_id) AS sl_count
FROM user_schemas us
JOIN servicelines sl
ON us.schema_id = sl.schema_id
WHERE us.user_id = 3
GROUP BY sl.schema_id;
any help is much appreciated!
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 02-07-2007, 06:48 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
It looks like you're pretty much on track-- what are you stuck on?
__________________
Fumigator is offline   Reply With Quote
Old 02-08-2007, 03:59 PM   PM User | #3
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
I'm trying to count the number of records in each table with one sql... I have this now but it's returning the number of rows in the process table for each count. eg: the count for ud_serviceline_id should be 9, but it's being returned as 40 which is the process_id count.

Code:
SELECT
us.description,
us.user_id,
count(sl.ud_serviceline_id) AS sl_count,
count(pr.process_id) AS pr_count
FROM user_schemas us
LEFT JOIN servicelines sl ON us.schema_id = sl.schema_id
LEFT JOIN processes pr ON pr.schema_id = us.schema_id
WHERE us.user_id = 3
GROUP BY sl.schema_id;
__________________
www.mattfacer.com
homerUK is offline   Reply With Quote
Old 02-08-2007, 06:30 PM   PM User | #4
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Ok I see now the problem. When you do a count(), it's going to return the number of total rows that would normally be returned by that query. With your JOIN query, you are going to get all of the rows from `serviceline` with matching ID, and all of the rows from `process` with matching ID. Selecting a count is, as you have found, going to return the total number of rows in the query result.

There is a way to narrow the count() in a result set using a subset of the result... maybe you could apply it here (I don't know if this will work):

Code:
SELECT
us.description,
us.user_id,
count(sl.ud_serviceline_id = us.schema_id OR NULL) AS sl_count,
count(pr.process_id = us.schema_id OR NULL) AS pr_count
FROM user_schemas us
LEFT JOIN servicelines sl ON us.schema_id = sl.schema_id
LEFT JOIN processes pr ON pr.schema_id = us.schema_id
WHERE us.user_id = 3;
BTW I really see no reason for the "GROUP BY" clause since the value of sl.schema_id will be the same for the entire result set.
__________________
Fumigator 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 02:46 AM.


Advertisement
Log in to turn off these ads.