2017-04-16 21 views
0

こんにちは私は各4つのテーブルから4つの値を表示するクエリを試行しています。MySQL select join null values

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'; 

ヌル部分以外はすべてが問題ありません。

テーブル構造

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

結果 "ジョン" の 'CS' と 'SS' はNULLです?場合、私はnullを印刷するにはどうすればよい

-------------------------------------------------- 
| 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    | 
-------------------------------------------------- 

あなたのコメントパー

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

よう

答えて

0

使用COALESCE()機能を使用すると、まったくdepartmentテーブルからname列出力を持っている必要はありませんように見えます。その場合、ただ

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

(OR)

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

ようにそれのために私はその機能を知っていただき、ありがとうございをNULLを選択します。私はメジャー(NULLの場合はcsまたはss)を隠そうとしています。私は '選択i.name、c.name、s.name、合体(d.name、 'NULL') インストラクターから私は'、動作しませんでした。 – Gomtting

+0

@Gomtting、それが助けあれば答えを編集してください。 – Rahul

+0

ああ、申し訳ありませんが、私は上記の名前から選んだとは思いませんでした..私はテーブルを編集しました。 cs/cs/ss/ssと同様に 'cs/NULL/NULL/ss'に変換します。 – Gomtting