Home > Database > Mysql Tutorial > body text

将一个表中的某列的值全部更新为与另一表中某一列的值相等

WBOY
Release: 2016-06-07 15:22:54
Original
1724 people have browsed it

数据库使用的是db2,其中有两个表,检查表(STUDY_TBL)和筛选表(SELECTION_TBL) ================================== STUDY_TBL的描述大致如下: STUDY_LID INTEGER PRIMARY KEY, STUDY_DATE DATE ... ... SELECTION_TBL的定义描述大致如下: SELECTION_L

数据库使用的是db2,其中有两个表,检查表(STUDY_TBL)和筛选表(SELECTION_TBL)

==================================

STUDY_TBL的描述大致如下:

STUDY_LID INTEGER PRIMARY KEY,

STUDY_DATE DATE

... ...

SELECTION_TBL的定义描述大致如下:

SELECTION_LID INTEGER PRIMARY KEY,

STUDY_LID INTEGER,

STUDY_DATE

... ...

=====================================

STUDY_LID在SELECTION_TBL中不是唯一的,现在要将SELECTION_TBL中的STUDY_DATE全部更新为与STUDY_TBL中的STUDY_DATE相同(也就是说,在这两张表中,STUDY_,LID相同的,SELECTION_TBL中的STUDY_DATE要保持与STUDY_TBL中的STUDY_DATE相同)。

sql语句如下:

UPDATE PACS.SELECTION_TBL SEL
SET STUDY_DATE = (SELECT ST.STUDY_DATE
FROM PACS.STUDY_TBL ST
WHERE ST.STUDY_LID = SEL.STUDY_LID)
WHERE EXISTS
(SELECT 1 FROM PACS.STUDY_TBL ST1 WHERE ST1.STUDY_LID = SEL.STUDY_LID)

PACS是模式名称,在以后的文章中,还要详细讨论exists的用法,敬请期待。。。。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template