How to output only certain items from a column in sql

T North :

I am writing an outer join in sql to search a few tables and the code basically works except that I need it to return only the persons twitter link so if they have another social media listed or no social media at all it should return null but right now it returns just the twitter for some but it returns null for some names that have a twitter. I know exactly what the problem is I think, in the on statements there is an and well if there is no advisor it will be null and if that person has a twitter then one value is null and one is not so it returns null. That is the only problem I can think of but when I use or it returns too many results and some are wrong. So I do not know how to fix this.

select fname, lname, rname, advisor_name, smaddr
from PERSON
join ADVISOR on PERSON.idnum = ADVISOR.student
left join DIGITAL on ADVISOR.student = DIGITAL.idnum and DIGITAL.smtype = 'twitter'
union
select fname,lname, rname, advisor_name, smaddr
from PERSON
left join ADVISOR on PERSON.idnum = ADVISOR.student
left join DIGITAL on ADVISOR.student = DIGITAL.idnum and
DIGITAL.smtype = 'twitter';

Current Output:

+----------+---------+-------------------------+---------------+---------------------------+
| fname    | lname   | rname                   | advisor_name  | smaddr                    |
+----------+---------+-------------------------+---------------+---------------------------+
| William  | South   | [email protected]   | Abby Tanner   | twitter.com/south.william |
| Terry    | Smith   | [email protected]     | Nicole Taylor | twitter.com/smith.terry   |
| Timothy  | Clemens | [email protected] | NULL          | NULL                      |
| Dan      | North   | [email protected]       | NULL          | NULL                      |
| Courtney | Cox     | NULL                    | NULL          | NULL                      |
+----------+---------+-------------------------+---------------+---------------------------+

PERSON

+-------+---------+----------+-------------------------+---------+----------+
| idnum | lname   | fname    | rname                   | private | linkblue |
+-------+---------+----------+-------------------------+---------+----------+
| 22222 | Clemens | Timothy  | [email protected] |       0 | tgcl258  |
| 40256 | South   | William  | [email protected]   |       1 | weso123  |
| 55555 | North   | Dan      | [email protected]       |       0 | ddno453  |
| 56732 | Cox     | Courtney | NULL                    |       1 | cco546   |
| 68123 | Smith   | Terry    | [email protected]     |       1 | tlsm321  |
+-------+---------+----------+-------------------------+---------+----------+

DIGITAL

+-------+----------+------------------------------+
| idnum | smtype   | smaddr                       |
+-------+----------+------------------------------+
| 22222 | facebook | facebook.com/clemons.timothy |
| 40256 | facebook | facebook.com/south.william   |
| 68123 | facebook | facebook.com/smith.terry     |
| 22222 | twitter  | twitter.com/clemons.timothy  |
| 40256 | twitter  | twitter.com/south.william    |
| 56732 | twitter  | twitter.com/cox              |
| 68123 | twitter  | twitter.com/smith.terry      |
+-------+----------+------------------------------+

ADVISOR

+---------+---------+---------------+---------------+-------+-------+
| student | advisor | student_name  | advisor_name  | sdate | edate |
+---------+---------+---------------+---------------+-------+-------+
|   40256 |   40256 | William South | Abby Tanner   | NULL  | NULL  |
|   68123 |   68123 | Terry Smith   | Nicole Taylor | NULL  | NULL  |
+---------+---------+---------------+---------------+-------+-------+

Expected output

+----------+---------+-------------------------+---------------+---------------------------+
| fname    | lname   | rname                   | advisor_name  | smaddr                    |
+----------+---------+-------------------------+---------------+---------------------------+
| William  | South   | [email protected]   | Abby Tanner   | twitter.com/south.william |
| Terry    | Smith   | [email protected]     | Nicole Taylor | twitter.com/smith.terry   |
| Timothy  | Clemens | [email protected] | NULL          | twitter.com/clemons.timothy                      |
| Dan      | North   | [email protected]       | NULL          | NULL                      |
| Courtney | Cox     | NULL                    | NULL          | twitter.com/cox                      |
+----------+---------+-------------------------+---------------+---------------------------+
GMB :

I don't see why you would need union here. Two left joins should get the job done:

select
    p.fname,
    p.lname,
    p.rname,
    a.advisor_name,
    d.smaddr
from person p
left join advisor a on a.student = p.idnum
left join digital d on d.idnum = p.idnum and d.smtype = 'twitter'

Demo on DB Fiddle:

fname    | lname   | rname                   | advisor_name  | smaddr                     
:------- | :------ | :---------------------- | :------------ | :--------------------------
Timothy  | Clemens | [email protected] | null          | twitter.com/clemons.timothy
William  | South   | [email protected]   | Abby Tanner   | twitter.com/south.william  
Dan      | North   | [email protected]       | null          | null                       
Courtney | Cox     | null                    | null          | twitter.com/cox            
Terry    | Smith   | [email protected]     | Nicole Taylor | twitter.com/smith.terry    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5579&siteId=1