Travis Bowen :
I am relatively new to SQL and I need to show a row that doesn't have a specific value in it without adding a value into a table for it. The tables I am working with are:
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 printout the name of the Name of the student, their email, the advisor's name, and their twitter. So something 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 |
+------------+-----------+-----------------+--------------------+-------------------+-------------------------+
Which 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 be showing two rows, but one of the rows doesn't have a twitter so it wouldn't show. I am looking for a way to show it.
GMB :
That 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'