how can i join multiple tables in one query
these are my tables
registration
registrationid
regschedid
studentid
registrationschedule
regschedid
session
sessionid
regschedid
sessiondate
schedules
scheduleid
regschedid
teacherid
faculty
teacherid
fname
I wanted to join them all so that i could get the fname and the session date please help me..
by the way i must specify the registration.studentid so that i could get the actual student
To join multiple tables, you use the same technique as to join 2 tables:
SELECT *
FROM registration
JOIN registrationschedule ON registration.regschedid = regschedid
JOIN student ON registration.studentid = student.studentid
--- etc
SELECT registration.*,registrationschedule.*, session.*,schedules.*,faculty.*
FROM registration
LEFT JOIN registrationschedule on registration.regschedid = registrationschedule.regschedid
LEFT JOIN session on session.regschedid = registrationschedule.regschedid
LEFT JOIN schedules on schedules.regschedid = session.regschedid
LEFT JOIN faculty on faculty.teacherid = schedules.teacherid
WHERE registrationid = 1;
SELECT * FROM registration
JOIN registrationschedule ON registration.regschedid=regschedid
JOIN session ON registration.regschedid=session.regschedid
JOIN schedules ON regschedid.regschedid=regschedid
JOIN faculty ON schedules.teacherid = faculty.techerid
WHERE student.id = ?
Related
My title isn't great, but let me explain my situation. I have a jobs table. The jobs table has 2 foreign keys to the users table: sales_rep_id and account_manager_id.
Then I have another table called contact_info with a one to one relationship to the users table.
jobs
-----
sales_rep_id
account_manager_id
...
users
-----
first_name
last_name
contact_info
-----
user_id
home_phone
If I want to do a query where I get the phone number for both people on every job I would do the following:
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home FROM jobs
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) reps
ON reps.id = jobs.sales_rep_id
JOIN
(SELECT * FROM users
JOIN contact_info
ON users.id = contact_info.user_id) account_managers
ON account_managers.id = jobs.account_manager_id
Is there anything I can do to create a temporary table with the joined data? What is the most efficient way to do this join? For example, what if I had 10 foreign keys in the jobs table to the users table, and I needed the phone_number for all 10?
This should be obtain all the info you need
SELECT j.*
, u1.first_name as sales_rep_first_name
, u1.last_name as sales_reps_last_name
, u2.first_name as manager_first_name
, u2.u1.last_name as manager_last_name
, c1.home_phone as sales_rep_home_phone
, c2.home_phone as manager_home_phone
FROM jobs as j
INNER JOIN contact_info as c1 ON j.sales_rep_id = c1.user_id
INNER JOIN user u1 ON u1.id= c1.user_id
INNER JOIN contact_info as c2 ON j.saccount_manager_id = c2.user_id
INNER JOIN user u2 ON u2.id= c2.user_id;
Define a view that joins user and contact_info.
CREATE VIEW user_contact_info
SELECT u.id, u.first_name, u.last_name, c.home_phone
FROM user AS u
JOIN contact_info AS c ON u.id = c.user_id
Then you can use this as if it's a table.
SELECT reps.home_phone as reps_home, account_managers.home_phone as a_m_home
FROM jobs
JOIN user_contact_info AS reps ON reps.id = jobs.sales_rep_id
JOIN user_contact_info AS account_managers ON account_managers.id = jobs.account_manager_id
Three tables courses,registration,students
columns in students
firstname,lastname,studentid,major,admitdate,graddate,gender,dob
columns in registration
courseid,studentid
columns in courses
coursenumber,coursename,credits
select statement I need to modify
select lastname as 'Last Name',sum(credits) as 'Credits Registered For' from students as s
inner join registration as r on s.studentid = r.studentid
inner join courses as c on c.coursenumber = c.courseid
group by last name;
the question on the lab is... Modify the previous query to show all students, even if they have not registered for a class. You should have 14 rows. Students who are not registered will show NULL in output.
I know this requires outer join of some sort but I'm not fully grasping these joins i've read multiple posts on here and other sites but can't seem figure it out.
use LEFT JOIN
select lastname as 'Last Name',
sum(credits) as 'Credits Registered For'
from students as s
LEFT join registration as r on s.studentid = r.studentid
LEFT join courses as c on c.coursenumber = r.courseid
group by last name;
try
SELECT s.lastname AS 'Last Name',
sum(c.credits) AS 'Credits Registered For'
FROM students s
LEFT JOIN registration r ON (s.studentid = r.studentid)
INNER JOIN courses c ON (c.coursenumber = r.courseid)
GROUP BY lastname;
note: no need to use AS while giving alias to table name
I am currently trying to run a query that will show all tables that correlate with each other. I did not make the table design. So I am running into some trouble in making: It's not clear how office_hours table correlates with schedule table? Overall how can I display properly all tables that correlate through a query?
SELECT *
FROM schedule
INNER JOIN semester ON schedule.semester_id = semester.id
INNER JOIN office_hours ON office_hours.id = schedule.???
I think ID from table schedule is only an auto_increment column and the proper way to join schedule from office_hours is office_hours.schedule_id = schedule.semester_id.
select *
from schedule
inner join semester
on schedule.semester_id = semester.id
inner join office_hours
on office_hours.schedule_id = schedule.semester_id
UPDATE 1
select *
from schedule
inner join semester
on schedule.semester_id = semester.id
inner join office_hours
on office_hours.schedule_id = schedule.semester_id
INNER JOIN faculty
ON faculty.id = office_hours.faculty_id
INNER JOIN Section
ON Section.faculty_ID = faculty.id AND
Section.Schedule_ID = Schedule.ID
INNER JOIN class
ON Class.ID = Section.Class_ID
INNER JOIN major_class_br
ON major_class_br.class_ID = Class.ID
INNER JOIN major_minor
ON major_class_br.major_minor_id = major_minor.ID
it is assumed that all ID or linking columns exists on each table that is why INNER JOIN was used. Otherwise, use LEFT JOIN.
You must use the id, which is in both tables:
...
inner join office_hours on office_hours.schedule_id = schedule.id;
Tryu this:
SELECT *
FROM
SCHEDULE
INNER JOIN semester
ON schedule.semester_id = semester.id
INNER JOIN office_hours
ON office_hours.schedule_id = schedule.id
I have a users table which contains the users information (fname, lname...etc) and a invoices table. In the invoices table I have a field called created_id which links to the user.id that created the invoice. I also have a field called staff_id which links to the staff user.id that approved the invoice.
How can I query the first and last name for both the created_id and the staff_id in a single query? Here are a few things I've tried....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname
FROM
invoices
INNER JOIN users
ON users.id = invoices.created_id;
This works, but it only gets me the person's name that created the invoice. How can I add the staff's name to that as well....
SELECT
invoices.*,
users.fname as created_fname,
users.lname as created_lname,
users2.fname as staff_fname,
users2.lname as staff_lname
FROM invoices, users
LEFT JOIN
invoices,
users AS users2
ON
users.id = invoices.created_id,
users.id = users2.id
That doesn't work, but is closer. Any guidance or examples would be very helpful. Also, if you have any recommendations for good books on learning how to do more advanced MySQL queries that would be helpful too.
You need to join users table twice on table Invoice.
SELECT a.*,
b.fname created_firstName,
b.lname created_LastName,
c.fname staff_firstName,
c.lname staff_LastName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
and best thing is you can concatenate their names into one using CONCAT
SELECT a.*,
CONCAT(b.fname, ' ', b.lname) created_fullName,
CONCAT(c.fname, ' ', c.lname) staff_fullName
FROM Invoice a
INNER JOIN users b
ON a.created_id = b.id
INNER JOIN users c
ON a.staff_id = c.id
I have the following database schema:
table courses:
id
tutor_id
title
table course_categories:
id
category_id
course_id
table categories:
id
name
table tutors:
id
name
table subscribers:
id
course_id
user_id
I need to make 1 sql to get a course with all it's categories, and the tutor for that course and the number of subscribers for that course. Can this be done in 1 query? Should this be done using stored procedures?
With this query you get what you want:
select co.title as course,
ca.name as category,
t.name as tutor,
count(s.*) as total_subscribers
from courses co
inner join course_categories cc on c.id = cc.course_id
inner join categories ca on cc.category_id = ca.id
inner join tutors t on co.tutor_id = t.tutor_id
left join subscribers s on co.id = s.course_id
where co.title = 'Cat1'
group by co.title, ca.name, t.name
I used left join on subscribers because there might be no one for a given course. I'm assuming that all the other tables have data on it for every course, categorie and tutor. If not, you can user left join as well but then you'll have data with null.
It can be done. You need to look up select and the use of join. See select and join to help complete the assignment
select cou.title, cat.name, tu.name, count(sub.user_id) from courses cou, course_categories cca, categories cat, tutors tu, subscribers sub where cou.id = cca.id and cat.id = tu.id and tu.id = sub.id group by cou.title, tu.name;