2012-12-19 81 views
0

我想通過比較u.fld_id = ct.fld_teacher_id 並且如果ct.fld_teacher_id爲空,我將顯示尚未分配的教師信息。但是具有空ct.fld_teacher_id的行在執行時不會顯示。Null以及Null值

這是我的查詢。

SELECT ct.fld_id as id, ct.fld_serial, ct.fld_city, ct.fld_venue_name, 
     ct.fld_start_time, ct.fld_end_time, 
     ct.fld_duration, ct.fld_available_seats, 
     ct.fld_course_price, ct.fld_status, 
     c.fld_name, u.fld_first_name, u.fld_last_name, 
     ct.fld_status, ct.fld_teacher_id, ct.fld_training_name, 
     ct.fld_class, ct.fld_level, ct.fld_is_fixed, tt.fld_name as training_type_name 
    FROM tbl_course_training as ct, tbl_users as u, 
     tbl_courses as c, tbl_training_types as tt 
    WHERE c.fld_id = ct.fld_course_id AND u.fld_id = ct.fld_teacher_id AND 
     ct.fld_teacher_id = 'NULL' AND tt.fld_id = ct.fld_training_type_id 
+1

您能向我們展示一些示例數據嗎? – Pranav

回答

0

我不能在不知道結構的情況下整理你的查詢。但是我注意到的事情很少。

  • ,如果你需要選擇即使ct.fld_teacher_id is null然後

    u.fld_id = isnull(ct.fld_teacher_id, u.fld_id) --or (sql-server) 
    u.fld_id = ct.fld_teacher_id or ct.fld_teacher_id is null --(my sql or sql-server) 
    u.fld_id = COALESCE(ct.fld_teacher_id, u.fld_id) --(my sql) 
    
  • 而且ct.fld_teacher_id = 'NULL'空 不是字符串

    ct.fld_teacher_id is null -- Correct way 
    
  • 你不能比較空值避免加入像這樣的表格的舊方法3210

    From Table1 t1 JOIN Table2 t2 On t1.Key = t2.Key --Correct way 
    
  • 我想你需要在這裏有幾個Left Join s。請check this link瞭解Sql Joins如何工作

+0

通過比較教學課程的u.fld = ct.fld_teacher_id,我得到了教師的詳細信息(姓名,電子郵件等)。 說一個課程已註冊,但教師尚未分配。 fld_teacher_id中的值將爲空。 – Mir

+0

@ user1863272:在這種情況下,您需要一個「左連接」。閱讀上面的鏈接瞭解聯接。 – Kaf