How can I justify rows that don't have a specific value?
Travis Bowen:
I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is:
Select * from PERSON;
+-------+-------+---------+--------+---------+----------+
| idnum | fname | lname | rname | private | linkblue |
+-------+-------+---------+--------+---------+----------+
| 11111 | Bob | Bobsley | Robert | 0 | bob1111 |
| 93210 | Bowen | Travis | Luke | 1 | tlbo232 |
+-------+-------+---------+--------+---------+----------+
Select * from ADVISOR;
+---------+---------+-------+-------+ You can ignore the sdate and edate
| student | advisor | sdate | edate |
+---------+---------+-------+-------+
| 11111 | 93210 | NULL | NULL |
| 93210 | 11111 | NULL | NULL |
+---------+---------+-------+-------+
Select * from DIGITAL;
+----------+-------+-------------------------+
| smtype | idnum | smaddr |
+----------+-------+-------------------------+
| email | 11111 | [email protected] |
| email | 93210 | [email protected] |
| facebook | 11111 | [email protected] |
| facebook | 93210 | [email protected] |
| twitter | 11111 | twitter.com/bob.bobsley |
+----------+-------+-------------------------+
The goal is to print out the name of the student, their email, the faculty advisor's name, and their Twitter name. So like this:
+------------+-----------+-----------------+--------------------+-------------------+-------------------------+
| First Name | Last Name | Email | Advisor First Name | Advisor Last Name | Advisor Twitter |
+------------+-----------+-----------------+--------------------+-------------------+-------------------------+
| Bowen | Travis | [email protected] | Bob | Bobsley | twitter.com/bob.bobsley |
+------------+-----------+-----------------+--------------------+-------------------+-------------------------+
It comes from:
select
P.fname as 'First Name',
P.lname as 'Last Name',
D.smaddr as 'Email',
AdivsorList.fname as 'Advisor First Name',
AdivsorList.lname as 'Advisor Last Name',
AdivsorList.smaddr as 'Advisor Twitter'
from PERSON P
LEFT OUTER JOIN DIGITAL D ON P.idnum = D.idnum
LEFT OUTER JOIN (
SELECT P1.fname, P1.lname, A.advisor, D1.smaddr, D1.smtype
from ADVISOR A
LEFT OUTER JOIN PERSON P1 ON A.student = P1.idnum
LEFT OUTER JOIN DIGITAL D1 ON P1.idnum = D1.idnum
) as AdivsorList ON P.idnum = AdivsorList.advisor
where D.smtype = 'email' AND AdivsorList.smtype = 'twitter';
The output should show two lines, but one of the lines is not twitter so it won't show up. I'm looking for a way to prove this.
GMB:
This can be done with a series of left join
s:
select
ps.fname student_fname,
ps.lname student_lname,
ds.smaddr student_email
pa.fname adviser_fname,
pa.lname adviser_lname,
da.smaddr advisor_twitter
from person ps
left join advisor a on a.student = ps.idnum
left join person pa on pa.idnum = a.advisor
left join digital ds on ds.idnum = ps.idnum and ds.smtype = 'email'
left join digital da on da.idnum = pa.idnum and da.smtype = 'twitter'