在SQL Server 2008中需要加入,我只是输了

时间:2015-01-20 21:41:28

标签: sql sql-server-2008

我在1992-1999的SQL编程。在SQL Server 2008中,我正在接受一项新的“培训”工作,因为我在15年内没有编程。

我知道我正在谈论“漫长的路程”。你能帮我看一下吗?

两张桌子:

LANG_MASTER

text_ID    Country_ID    Master_Text
------------------------------------
  45          166        Counta
  45           65        Count
  67          166        Region
  67           65        Reginia
  78           65        Field
  78          166        Field

NEW_TRANSLATIONS

English      Translation    Country ID    Status
------------------------------------------------
Count        Countee           166           0
Region       Provinicia        166           0
Field        Felda             166           0
House        Casa              166           0

我需要将New_Tranlations.status更新为3,如下所示:

  • New-Translations.English = Lang_Master.Master_Text ..........(Text_ID = X)
  • Lang_Master.County_ID = 166 ....... Text_ID = X
  • New_translations.translation<> Lang_master.master_text

基本上如果主表中有翻译,但它与new_translation表中的翻译不同,我需要将其标记为'3'

e.g。结果要求:

NEW_TRANSLATIONS

English      Translation    Country ID     Status
-------------------------------------------------
Count        Countee           166            3
Region       Provinicia        166            3
Field        Felda             166            0
House        Casa              166            0

据我所知:

update new_translations
set status = 3 
where translation in
    (select LM.master_text
    from lang_master as LM
        , new_translations as NT
     where  NT.english <> LM.text_ID
      and LM.country_id = 65  
     and LM.text_ID in 
  ( select LM.text_id 
    from lang_master as LM
         ,new_translations as NT
    where LM.master_text = NT.translation
    and LM.country_id = 166))

我知道这是错的,但我不知道该怎么办!请帮忙!

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,您的查询应如下所示:

UPDATE new_translation n1 SET status = 3 WHERE EXISTS
(
   SELECT m1.text_id, m1.country_id c1, m1.master_text mt1, m2.country_id c2, m2.master_text mt2 
   FROM lang_master m1 INNER JOIN lang_master m2
   ON m1.text_id = m2.text_id and m1.country_id > m2.country_id
   WHERE n1.country_id = m1.country_id AND m1.master_text <> n1.translation 
     AND m2.master_text = n1.english 
);

这是小提琴试试:http://sqlfiddle.com/#!2/ab3c0d/1

答案 1 :(得分:0)

我认为您可以根据lang_masternew_translations

之间的以下常用字段加入
text_id
Country_id

然后以下查询可以提供帮助:

update NT 
set status = 3 
FROM lang_master lm JOIN new_translations nt
ON lm.text_id = nt.text_id
and lm.Country_ID = nt.Country_ID
and lm.master_text <> nt.translation

如果连接列不同,只需更新连接条件即可。但或多或少,上面的内容可以完成这项任务。