Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL Joins statements

$
0
0
I am trying to write an MysQL statement that requires me to pull
data from different tables. I wrote a statement and used JOINs to link
tables and extract data.

I have a table called people. It lists the fname of person and the position-Volunteer or Staff.  A volunteer is assigned to a staff member to perform a task.  I want to view the task, the volunteer, and the person assigned to that task.

I assign a volunteer to the task. When the volunteer is done with the task, a staff member is then assigned to the task.  I keep separate tables for this-one that shows the volunteer and task assigned to the volunter and a second that shows which staff was assigned to the task.

People(PID,Fname,Position);
Volunteer(AssignID,PID,Task);
Staff(RevID,AssignID,PID);-it tells which assignment and volunteer the staff member is assigned to.

I want to produce a table like this:
(AssignID,Volunteer,Staff, TASK)

it shows the volunteer's and staff's name.  

SELECT `volunteer`.`assignID`,`people`.`fname`,`staff`.`pid`,`volunteer`.`task` FROM `volunteer` JOIN
`people` ON `volunteer`.`pid`=`people`.`pid`;

I am having a problem getting just the staff name. I do not want the staff members ID number as shown but the staff member's actual name. The name is in the people table.

1. I need to get the firstname of the staff member. The select statement above lets me get only the firstname for the volunteer because the volunteer table is joined to the people table.  

2. How can I get the firstname of the staff member without having to change my table columns.

I was going to use a UNION statement and write another select statement

SELECT `volunteer`.`assignID`,`people`.`fname`,`staff`.`pid`,`volunteer`.`task` FROM `volunteer` JOIN `people` ON `volunteer`.`pid`=`people`.`pid` UNION SELECT `people`.`fname` FROM `people` JOIN `staff` ON `people`.`pid`=`staff`.`pid`;

but it says I cannot do this because the select statements have a different number of columns.  I also cannot re-join the people table, like shown above, to the staff table
because it already exists in the jon statements.

How can I do this?







Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>