2017-04-16 1 views
0

Hallo, ich versuche eine Abfrage, die 4 Werte aus jeder 4 Tabellen zeigen wird.MySQL wählen Join null Werte

select i.name, c.name, s.name, d.name 
from instructor i 
join course c 
    on i.pid = c.instructor 
join course_taken ct 
    on c.id = ct.cid 
join student s 
    on s.id = ct.sid 
join department d 
    on s.major = d.id or s.major is null 
where i.name = 'lee'; 

alles ist gut, außer für den Nullteil.

Tabellenstruktur

-------------------------------------------------- 
| student | course | instructor | department | 
-------------------------------------------------- 
| name | name  | id   | id   | 
-------------------------------------------------- 
| id  | id  | name  | name   | 
-------------------------------------------------- 
| major |instructor| department |    | 
-------------------------------------------------- 

das Ergebnis

-------------------------------------------------- 
| i.name | c.name | s.name | d.name   | 
-------------------------------------------------- 
| kim  | math | jack | cs    | 
-------------------------------------------------- 
| kim  | math | john | cs    | --> THIS VALUE IS 
-------------------------------------------------- 
| kim  | math | john | ss    | --> NULL AND SHOULD BE PRINTED IN NULL 
-------------------------------------------------- 
| kim  |math  | json | ss    | 
-------------------------------------------------- 

Wie ich null drucken kann, wenn 'cs' und 'ss' in "John" NULL ist?

Antwort

0

USE COALESCE() Funktion wie

select coalesce(i.name,'NULL') as iName, ... 
from instructor i 

Per Ihren Kommentar sieht aus wie Sie nicht auf alle name Spalte Ausgabe von department Tabelle haben wollen. In diesem Fall nur NULL wählen für sie

select i.name, c.name, s.name, null 

mögen (OR)

select i.name, c.name, s.name, 'null' 
+0

Danke, dass ich diese Funktion kennen. Ich versuche, Major (cs oder ss wenn NULL) zu verbergen. Ich habe versucht 'wählen i.name, c.name, s.name, coalesce (d.name, 'NULL') von Instruktor i ', hat nicht funktioniert. bekommt immer noch 'cs' und 'ss' in der großen Spalte :( – Gomtting

+0

@Gomtting, siehe Bearbeiten in der Antwort, wenn das hilft. – Rahul

+0

Oh sorry, ich glaube, ich wusste nicht über Name ich wählte von oben .. ich bearbeitet die Tabelle Ich brauche große Namen, auch wenn es NULL ist. Wie 'cs/cs/ss/ss' in' cs/NULL/NULL/ss'. Danke – Gomtting