SQL updates multiple fields with subquery results. SQL queries table fields.

Source: Internet
Author: User

SQL updates multiple fields with subquery results. SQL queries table fields.

Author: iamlasong

Requirement: The table content needs to be changed. The changed content should be placed in a temporary table, and multiple fields in the formal table should be updated using SQL statements.

If you update a field, you can directly use the field name = subquery. Update multiple fields and write them out in parentheses as follows:

Update tb_jg t
Set t. jgfl = 'sd ',
(T. zj_code, t. zj_mc) = (select a. zj_code, a. zj_mc
From song_temp
Where a. zj_code = t. zj_code)
Where exists (select 1 from song_temp a where a. zj_code = t. zj_code)

Requirement: Create a table based on the subquery. The statement is as follows:

Create table sncn_zd_jg as select * from tb_jg where zdbz = '1' order by city_code, xs_code;

If 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;



Multiple values of a field in SQL query are common:
Select * from t1
Where exists (select * from (select name, number, max (money) as money from t1 group by name, number) as tmp
Where name = t1.name and number = t1.number
And money = t1.money)
In SQL server, when multiple tuples with subqueries are updated at the same time, the preceding error occurs: The returned value indicates that the subquery end returns multiple fields and only accepts the fields (that is, the value =: indicates a single field) = changed to INOK
Update dd
Set dprice IN (select sum (book. price * orderdetail. number) from orderdetail, book, dd
Where book. bookno = orderdetail. bookno and dd. ddno = orderdetail. ddno
Group by dd. ddno)
From orderdetail, book
Where orderdetail. bookno = book. bookno
------------------------------------

Related Article

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.