从另一个表

时间:2016-07-19 00:58:30

标签: mysql

我想从表西班牙语更新MySQL表cp5sql,并在PhpMyadmin中使用以下语句。字段名称是正确的,尽管每个表格略有不同。该声明的最后一部分是将更新限制为单词“能够”。只是为了测试。

我收到第2行出错的错误。

UPDATE cp5sql c
SET (Spanish_ID, Spanish_Type,Spanish_Uoffset,Spanish_Synset,Spanish_Word)=
(SELECT SID,type,offset,synset,word FROM spanish s
WHERE s.type=c.Type AND s.synset=c.synset AND c.Word ='able');

我也尝试过:

UPDATE cp5sql
SET (Spanish_ID, Spanish_Type,Spanish_Uoffset,Spanish_Synset,Spanish_Word)=
(SELECT SID,type,offset,synset,word FROM spanish
WHERE spanish.type=cp5sql.Type AND spanish.synset=cp5sql.synset AND cp5sql.Word ='able');

2 个答案:

答案 0 :(得分:0)

你有一个很好的猜测,但它实际上是UPDATE FROM语法 像这样:

UPDATE
    Table_A
SET
    Table_A.col1 = Table_B.col1,
    Table_A.col2 = Table_B.col2
FROM
    Some_Table AS Table_A
    INNER JOIN Other_Table AS Table_B
        ON Table_A.id = Table_B.id
WHERE
    Table_A.col3 = 'cool'

尝试将您的示例变为这样的语法:

UPDATE cp5sql c
SET Spanish_ID = SID, 
  Spanish_Type = type,
  Spanish_Uoffset = offset,
  Spanish_Synset = synset,
  Spanish_Word = word
FROM spanish s
WHERE s.type = c.Type
  AND s.synset = c.synset
  AND c.Word ='able';

答案 1 :(得分:0)

您必须使用UPDATE JOIN

UPDATE cp5sql c
JOIN spanish s
ON s.type = c.Type AND s.synset = c.synset AND c.Word = 'able'
SET 
    c.Spanish_ID = s.SID,
    c.Spanish_Type = s.type,
    c.Spanish_Uoffset = s.offset,
    c.Spanish_Synset = s.synset,
    c.Spanish_Word = s.word;