SQL中update与update select结合语法详解与实例
1、通用update
一般简单的update语法比较通用
语法:
UPDATE table_name
SET column1=value1,
column2=value2,
...
WHERE some_column = some_value;
12345
注:若不加where条件则是更新表中的所有数据,
故执行没有where子句的update要慎重再慎重。
实例:
UPDATE subject
SET name='数学',
type='理学'
WHERE id = 1;
1234
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];
12345
注:若不加where条件则是更新表中的所有数据,
故执行没有where子句的update要慎重再慎重。
实例:
UPDATE table1 t1
SET column1 = t2.columnname1
column2 = t2.columnname2
FROM (select columnname1,columnname2 from table2) t2
WHERE t1.column3 = t2.column3
AND t1.column = '111';
123456
注:对于set列中左边的列不能使用t1.这种别名方式,只能使用column名称
PostgreSQL与GreenPlum语法基本一致
3、MySQL update与select结合
第一种:
语法:
UPDATE table1 SET column1 = (SELECT column FROM table2 [WHERE condition])
WHERE table1.column2 = value;
12
注:若不加where条件则是更新表中的所有数据,
故执行没有where子句的update要慎重再慎重。
实例:
UPDATE tb_bookcase
SET name = (SELECT bookname FROM tb_bookinfo WHERE tb_bookinfo.type = tb_bookcase.type
AND tb_bookinfo.ord_date IN (SELECT MAX(ord_date) FROM tb_bookinfo))
WHERE tb_bookcase.subject = '理学';
1234
第二种:
语法:
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];
12345
实例:
例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;
1234
例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;
123456
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 ;
123456
4、Oracle update与select结合
语法:
UPDATE table1 alias
SET (column_name,column_name ) = (
SELECT column_name, column_name FROM table2 WHERE table2.column_name = alias.column_name)
[WHERE column_name = VALUE]
1234
注:若不加where条件则是更新表中的所有数据,
故执行没有where子句的update要慎重再慎重。
实例:
例1:
UPDATE t_order t1
SET (ordername,orderprice) = (SELECT detailname,totalprice FROM t_detail WHERE t_detail.detailclasses = t1.classes)
WHERE t1.orderid = 1
123
例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)
12
5、SQLServer update与select结合
语法:
UPDATE table1
SET column1 = t2.column1,
column1 = t2.column2,
...
FROM table1 INNER/LEFT/RIGHT JOIN table2 ON table1.column = table2.column
[WHERE conditions]
123456
注:若不加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;
12345678
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,t_order.order_code,t_detail.express_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;