Sql 在Postgres中使用自引用更新表会更新比预期更多的条目
我有一个表,其中的问题引用了同一个关于家长问题的表 对列Sql 在Postgres中使用自引用更新表会更新比预期更多的条目,sql,postgresql,sql-update,Sql,Postgresql,Sql Update,我有一个表,其中的问题引用了同一个关于家长问题的表 对列title的所有行条目执行SELECT查询时,将所有问题显示为下面的查询1/结果1对 查询1: SELECT t_child.title AS t_child_title, t_child.question AS t_child_question, t_child.parent_qid AS t_child_parent_qid, t_child.language AS t_child_language,
title
的所有行条目执行SELECT查询时,将所有问题显示为下面的查询1/结果1对
查询1:
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+-----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou Cóccix | 1095 | pt-BR | lisCortAt | 1095 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1095 | en | lisCortAt | 1095 | en
SacroCoccix | Sacrum and/ or coccyx | 1078 | en | lisFxAt | 1078 | en
SacroCoccix | Sacro e/ou Cóccix | 1078 | pt-BR | lisFxAt | 1078 | pt-BR
SacroCoccix | Sacro e/ou Cóccix | 1056 | pt-BR | lisCortPr | 1056 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1056 | en | lisCortPr | 1056 | en
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix' AND t_parent.title = 'lisFxPr';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
结果1:
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+-----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou Cóccix | 1095 | pt-BR | lisCortAt | 1095 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1095 | en | lisCortAt | 1095 | en
SacroCoccix | Sacrum and/ or coccyx | 1078 | en | lisFxAt | 1078 | en
SacroCoccix | Sacro e/ou Cóccix | 1078 | pt-BR | lisFxAt | 1078 | pt-BR
SacroCoccix | Sacro e/ou Cóccix | 1056 | pt-BR | lisCortPr | 1056 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1056 | en | lisCortPr | 1056 | en
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix' AND t_parent.title = 'lisFxPr';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
将过滤器t_parent='lisFxPr'
添加到查询中,结果仅限于问题父标题lisFxPr
,如下面的成对查询2/Result 2所示
查询2:
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+-----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou Cóccix | 1095 | pt-BR | lisCortAt | 1095 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1095 | en | lisCortAt | 1095 | en
SacroCoccix | Sacrum and/ or coccyx | 1078 | en | lisFxAt | 1078 | en
SacroCoccix | Sacro e/ou Cóccix | 1078 | pt-BR | lisFxAt | 1078 | pt-BR
SacroCoccix | Sacro e/ou Cóccix | 1056 | pt-BR | lisCortPr | 1056 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1056 | en | lisCortPr | 1056 | en
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix' AND t_parent.title = 'lisFxPr';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
结果2:
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+-----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou Cóccix | 1095 | pt-BR | lisCortAt | 1095 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1095 | en | lisCortAt | 1095 | en
SacroCoccix | Sacrum and/ or coccyx | 1078 | en | lisFxAt | 1078 | en
SacroCoccix | Sacro e/ou Cóccix | 1078 | pt-BR | lisFxAt | 1078 | pt-BR
SacroCoccix | Sacro e/ou Cóccix | 1056 | pt-BR | lisCortPr | 1056 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 1056 | en | lisCortPr | 1056 | en
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
SELECT t_child.title AS t_child_title,
t_child.question AS t_child_question,
t_child.parent_qid AS t_child_parent_qid,
t_child.language AS t_child_language,
t_parent.title AS t_parent_title,
t_parent.qid AS t_parent_qid,
t_parent.language as t_parent_language
FROM lime_questions AS t_child JOIN lime_questions AS t_parent
ON t_child.parent_qid = t_parent.qid AND t_child.language = t_parent.language
WHERE t_child.title = 'SacroCoccix' AND t_parent.title = 'lisFxPr';
t_child_title | t_child_question | t_child_parent_qid | t_child_language | t_parent_title | t_parent_qid | t_parent_language
---------------+----------------------+--------------------+------------------+----------------+--------------+-------------------
SacroCoccix | Sacro e/ou cóccix | 973 | pt-BR | lisFxPr | 973 | pt-BR
SacroCoccix | Sacrum and/or coccyx | 973 | en | lisFxPr | 973 | en
我只想更新结果2中显示的两行
我正在运行以下查询:
UPDATE lime_questions t_main SET title = 'SacrumCoccyx'
FROM lime_questions AS t_child
JOIN lime_questions AS t_parent
ON t_parent.qid = t_child.parent_qid AND t_parent.language = t_child.language
WHERE t_main.title = 'SacroCoccix' AND t_parent.title = 'lisFxPr';
但是,此UDPATE查询将更新结果1中显示的所有8个条目
我缺少什么?请更新语法:
[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
SET { column = { expression | DEFAULT } |
( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
[ FROM from_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
您可以尝试此查询
UPDATE lime_questions AS t_main
SET title = 'SacrumCoccyx'
FROM lime_questions AS t_parent
WHERE t_main.title = 'SacroCoccix' AND
t_parent.title = 'lisFxPr' AND
t_parent.qid = t_main.parent_qid AND
t_parent.language = t_main.language;
第一种选择(“您可以尝试此查询”)导致
错误:关系“lime\u questions”的“t\u main”列不存在
<代码>第2行:设置t_main.title='sacrumcocyx'第二个备选方案(“或使用别名更新”)导致错误:关系“t_main”不存在
<代码>第1行:更新t_main。就这样。非常感谢@D-Shih。几个小时的努力。@Caco没问题:)