字符串命令在sql中失败?

时间:2011-11-16 19:53:27

标签: sql postgresql

我只是试图截断Subjects.subjectid中varchar字段的前两个字符作为指定的记录子集,但它不起作用。我无法发现我的代码(postgresql)出了什么问题:

UPDATE subjects
SET
    subjectid = substring(S.subjectid from 2)
FROM
  ibg_studies ST,subjects S,dnasample D
WHERE 
    D.studyindex=ST.studyindex
    AND ST.studyabrv='CONGER'
    AND D.subjectidkey=S.id
    AND D.projectindex IS NULL

任何想法都赞赏。

2 个答案:

答案 0 :(得分:1)

您的子查询未与正在更新的表耦合(无核心)。 我不知道你的意图是什么,但也许你想要这个(只是猜测):

UPDATE subjects sj
SET
   subjectid = substring(S.subjectid from 2) -- << what is this?
FROM
  ibg_studies st
  -- ,subjects s
  ,dnasample d
WHERE d.studyindex = st.studyindex
  AND st.studyabrv = 'CONGER'
  AND d.subjectidkey = sj.id  -- changed from s to sj?
  AND d.projectindex IS NULL
    ;

答案 1 :(得分:0)

问题似乎是SQL在同一字段上调用字符串函数时更新字段时出现问题。我通过在表中添加一个列来克服限制,使用字符串函数更新这个新列,然后将新列值复制到原始目标字段中,如下所示:

-- 6: copy modified subjectid to temp
--UPDATE dnasample D
--SET
--  temp = substring(S.subjectid from 3)
--FROM
--  ibg_studies ST,subjects S
--WHERE 
--  D.studyindex=ST.studyindex
--  AND ST.studyabrv='CONGER'
--  AND D.subjectidkey=S.id
--  AND D.projectindex IS NULL

-- 7: copy temp to subjectid
--UPDATE subjects S
--SET
--  subjectid = D.temp
--FROM
--  ibg_studies ST,dnasample D
--WHERE 
--  D.studyindex=ST.studyindex
--  AND ST.studyabrv='CONGER'
--  AND D.subjectidkey=S.id
--  AND D.projectindex IS NULL

-- 8: Remove the temp column
ALTER TABLE dnasample DROP COLUMN temp