我有兩個表 - table1和table2。這兩個表的當前條件如下:更新SQL Server 2008中源表中多個匹配行的查詢?
表1:
id type mon tue wed thu fri sat sun
1 ets NULL NULL NULL NULL NULL NULL NULL
1 ets NULL NULL NULL NULL NULL NULL NULL
1 eta NULL NULL NULL NULL NULL NULL NULL
1 eta NULL NULL NULL NULL NULL NULL NULL
1 cl NULL NULL NULL NULL NULL NULL NULL
1 cl NULL NULL NULL NULL NULL NULL NULL
2 ets NULL NULL NULL NULL NULL NULL NULL
2 ets NULL NULL NULL NULL NULL NULL NULL
2 eta NULL NULL NULL NULL NULL NULL NULL
2 eta NULL NULL NULL NULL NULL NULL NULL
2 cl NULL NULL NULL NULL NULL NULL NULL
2 cl NULL NULL NULL NULL NULL NULL NULL
表2:
id ets eta cl
1 mon tue wed
1 thu fri sat
1 sun mon tue
2 sat sun mon
2 fri sat sun
注意,各列的表2中的名稱在表1的列「類型」。 我想更新table1,使列mon-sun得到table2中各個'type'(即ets,eta或cl)中找到的所有值的更新,而table1.id應該匹配table2.id。
所得表我想是象下面對上述數據:
id type mon tue wed thu fri sat sun
1 ets 1 NULL NULL 1 NULL NULL 1
1 ets 1 NULL NULL 1 NULL NULL 1
1 eta 1 1 NULL NULL 1 NULL NULL
1 eta 1 1 NULL NULL 1 NULL NULL
1 cl NULL 1 1 NULL NULL 1 NULL
1 cl NULL 1 1 NULL NULL 1 NULL
2 ets NULL NULL NULL NULL 1 1 NULL
2 ets NULL NULL NULL NULL 1 1 NULL
2 eta NULL NULL NULL NULL NULL 1 1
2 eta NULL NULL NULL NULL NULL 1 1
2 cl 1 NULL NULL NULL NULL NULL 1
2 cl 1 NULL NULL NULL NULL NULL 1
的UPDATE查詢我申請是如下:
update a
set a.mon = case b.ets when 'mon' then '1' else '0' end,
a.tue = case b.ets when 'tue' then '1' else '0' end,
a.wed = case b.ets when 'wed' then '1' else '0' end,
a.thu = case b.ets when 'thu' then '1' else '0' end,
a.fri = case b.ets when 'fri' then '1' else '0' end,
a.sat = case b.ets when 'sat' then '1' else '0' end,
a.sun = case b.ets when 'sun' then '1' else '0' end
from table1 a, table2 b
where a.id = b.id and a.type = 'ets'
考慮第一更新表table1.type = 'ets'
和的各自的值來自table2的id與ID同時匹配。
上述查詢僅從table2中獲取第一個匹配值,並更新table1中的值,而不是其他值。
任何幫助將不勝感激。
什麼地方a.rownum = b.rownum?沒有這樣的專欄'rownum'。 –
它是同一個colum id。我錯誤地鍵入rownum istead of id。 –
在表1中所有行都是重複的。爲什麼? –