SQL中update与updateselect结合语法详解与实例SQL中update与update select结合语法详解与实例
1、通⽤update
⼀般简单的update语法⽐较通⽤
语法:
UPDATE table_name
SET column1=value1,
column2=value2,
...
WHERE some_column = some_value;
注:若不加where条件则是更新表中的所有数据,
故执⾏没有where⼦句的update要慎重再慎重。
实例:
UPDATE subject
SET name='数学',
type='理学'
WHERE id = 1;
2、GreenPlum update与select结合
语法:
UPDATE [only] table [[as] alias]
SET {column = {expression | default} |
(column [, ...]) = ({expression | default}[, ...])}[, ...]
[FROM fromlist]
[WHERE condition | WHERE current of cursor_name];
注:若不加where条件则是更新表中的所有数据,
故执⾏没有where⼦句的update要慎重再慎重。
实例:
UPDATE table1 t1
SET column1 = t2.columnname1
column2 = t2.columnname2
FROM (select columnname1,columnname2 from table2) t2
lumn3 = t2.column3
lumn = '111';
注:对于set列中左边的列不能使⽤t1.这种别名⽅式,只能使⽤column名称
PostgreSQL与GreenPlum语法基本⼀致
3、MySQL update与select结合
第⼀种:
语法:
UPDATE table1 SET column1 = (SELECT column FROM table2 [WHERE condition])
lumn2 = value;
注:若不加where条件则是更新表中的所有数据,
故执⾏没有where⼦句的update要慎重再慎重。
实例:
sql中update什么意思UPDATE tb_bookcase
SET name = (SELECT bookname FROM tb_bookinfo WHERE pe = pe
AND d_date IN (SELECT MAX(ord_date) FROM tb_bookinfo))
WHERE tb_bookcase.subject = '理学';
第⼆种:
语法:
UPDATE table1 inner/left/right join table2/(select columns from table3
[inner/left/right join on condition]  [where conditions]) as t3
ON condition
SET column1 = value1,column2 = value2,...
[WHERE conditions];
实例:
例1:
UPDATE $table1 a INNER JOIN $table2 b
ON a.user_id = b.user_id
SET  a.balance = a.balance + b.income,b.status= 1
WHERE  b.id = 5 AND b.status = 0;
例2:
UPDATE A INNER JOIN
(SELECT B.B1 as B1,B.B2 as B2,C.C1 as C1 from B LEFT JOIN C on
B.B3=
C.C3) as t
ON A.A3=t.B1
set A.A1=t.B2,
A.A2=t.C1;
UPDATE tb_bookcase INNER JOIN
(SELECT tb_bookinfo.rid as rid,tb_bookinfo.bookname,department.name LEFT JOIN department on
tb_bookinfo.depid=department.id) as t
ON tb_bookcase.id=t.rid
set tb_bookcase.bookname=t.bookname,
tb_bookcase.departname=t.name ;
4、Oracle update与select结合
语法:
UPDATE table1 alias
SET (column_name,column_name ) = (
SELECT column_name, column_name FROM table2 lumn_name = lumn_name) [WHERE column_name = VALUE]
注:若不加where条件则是更新表中的所有数据,
故执⾏没有where⼦句的update要慎重再慎重。
实例:
例1:
UPDATE t_order t1
SET (ordername,orderprice) = (SELECT detailname,totalprice FROM t_detail WHERE t_detail.detailclasses = t1.classes) derid = 1
例2:
UPDATE A SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID) WHERE ID IN (SELECT B.ID FROM B WHERE A.ID = B.ID)
5、SQLServer update与select结合
语法:
UPDATE table1
SET column1 = t2.column1,
column1 = t2.column2,
...
FROM table1 INNER/LEFT/RIGHT JOIN table2 lumn = lumn [WHERE conditions]
注:若不加where条件则是更新表中的所有数据,
故执⾏没有where⼦句的update要慎重再慎重。
实例:
UPDATE A
SET A1 = t2.B2 ,
A2 = t2.C1
FROM A INNER JOIN (
SELECT  B.B1,B.B2,C.C1
FROM B left join C on B.B3 = C.C3) t2
ON A.A3 = t2.B1
WHERE A.A4 = 1;
UPDATE t_abnormal_fee
SET order_code = t2.order_code ,
return_fee = t2.express_fee
FROM t_abnormal_fee INNER JOIN (
SELECT  t_order.id,der_code,press_fee
FROM t_order left join t_detail on t_order.name = t_detail.name) t2
ON t_abnormal_fee.id = t2.id
WHERE t_abnormal_fee.id = 1;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。