SQL updates multiple fields with sub-query results

Source: Internet
Author: User

Author: Iamlasong

Requirements: Table content needs to be changed, changes in temporal table content, use of SQL official statement to update the table in several areas.

Suppose that you update a field directly with the field name = subquery, multiple fields are updated, and the fields are written in parentheses, such as the following:

Update TB_JG T
Set t.jgfl = ' SD ',
(T.zj_code, T.ZJ_MC) = (select A.zj_code, A.ZJ_MC
From Song_temp A
where A.zj_code = T.zj_code)
where exists (select 1 from song_temp a where a.zj_code = T.zj_code)

Requirements: Create a table from a subquery, such as the following:

CREATE TABLE SNCN_ZD_JG as SELECT * from TB_JG where zdbz= ' 1 ' ORDER by City_code,xs_code;

Suppose the table already exists. The following statement inserts the query result:
INSERT INTO SNCN_ZD_JG select * from TB_JG where zdbz= ' 1 ' ORDER by City_code,xs_code;


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

SQL updates multiple fields with sub-query results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.