1
   

MySQL help

 
 
stuh505
 
Reply Tue 14 Sep, 2004 10:07 pm
I'm having trouble figuring out how to use SQL to select when there are multiple levels of keys that link tables together.

For example, look at the following table structure in a database:

tblUserDepartments
field: deptID
field: userID

tblUserInfo
field: userID
field: userName
field: activityID

tblActivityPlan
field: activityID
field: activityName
field: plannedCount
field: actualCount

Now, I want to specify a deptID, and have it find each user that is in that department, and then find each activity plan for each user, and then create a record for each distinct activity...showing the planned and actual count for those activities.

Example desired recordset output:

UserName | ActivityName | Planned Count | Actual Count
Joe Smith | Social Work | 4 | 2
Joe Smith | Other Work | 3 | 3
Margaret R | Freel Work | 5 | 6

I don't see how I can use the general SQL queries when there are multiple levels of selecting necessary like this.

I could start with something like:

SELECT userID INTO tempUsers
FROM tblUserDepartments
WHERE tblUserDepartments.deptID = MyDeptID

SELECT userName, activityID INTO tempActivities
FROM tblUserInfo
WHERE tblUserInfo.userID = tempUsers.userID

SELECT activityName, plannedCount, actualCount
FROM tblActivityPlan
WHERE tblActivityPlan.activityID = tempActivities.activityID

I'm starting to confuse myself...this isn't right. I am thinking that somehow I should be using JOIN.
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 1 • Views: 716 • Replies: 3
No top replies

 
Craven de Kere
 
  1  
Reply Wed 15 Sep, 2004 12:07 am
Halfway down the post I skipped down to tell you:

Use JOIN

But I see that you are already suspecting as much.

Note, despite wanting to do more I probably won't have much time to tell you more than that.
0 Replies
 
stuh505
 
  1  
Reply Wed 15 Sep, 2004 08:18 am
is this right? I'm not sure what the difference is between what I put in the ON conditional versus the WHERE conditional...would it make a difffernce if i swapped them?


SELECT tblUserInfo.userName, tblActivityPlan.activityName, tblActivityPlan.plannedCount, tblActivityPlan.actualCount
FROM tblUserInfo
INNER JOIN tblUserDepartments
ON tblUserInfo.userID = tblUserDepartments.userID
WHERE tblActivityPlan.activityID = tblUserInfo.activityID
0 Replies
 
stuh505
 
  1  
Reply Wed 15 Sep, 2004 08:57 am
or is it more like this?

SELECT u.UserName AS User,
a.activityName AS Activity,
a.plannedCount AS Planned,
a.actualCount AS Actual
FROM ???
JOIN tblUserDepartments d ON d.deptID = 555
JOIN tblUserInfo u ON u.userID = d.userID
JOIN tblActivityPlan a ON a.activityID = u.activityID

no idea what to be taking it FROM though...
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » MySQL help
Copyright © 2024 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 04/29/2024 at 03:41:28