返回信息流☆─────────────────────────────────────☆
MrK (Mr.K) 于 (Wed Sep 7 20:53:26 2011) 提到:
现有两表格式如下:
表a:
id column1 column2 value
表b
column1 column2 value
其中表a的column1、column2在表b中总存在且唯一 表a中value字段的值都为空 表b中value字段是有值的
想将表a(行遍历一遍)对每行数据 找到表b中column1、column2值相同的那一行 将那行的value值赋给表a中相应的value
怎么实现呢?
在下新手 想
update value
from a
where (select)
这种方法 但是怎么select呢?直接a.column1=b.column1 and a.column2=b.column2 这得到的是所有的集合啊~~ 往各位大牛指点
☆─────────────────────────────────────☆
feng3982315 (大灰狼) 于 (Wed Sep 7 23:01:49 2011) 提到:
电脑没装数据库,SQL SERVER这样的话应该是没问题的,你试试
update result set value = b.value from a as result inner join b on result.column1 = b.column1 and result.column2 = b.column2
☆─────────────────────────────────────☆
doubleKO (九头鸟龙) 于 (Wed Sep 7 23:54:47 2011) 提到:
"询问SQL语句的问题也最好说明你正在或想在何种数据库后台产品(如MySQL, SQL Server, Access, Oracle, Sybase, DB2等)中运行该SQL语句。由于每种产品支持的SQL在细节上是不同的,因此不指明这一点可能会让人无法作答。"
不同数据库的多表查询更新还是有一定区别的。
oracle中
UPDATE <table_name>
SET <column_name> = (
SELECT <column_name>
FROM <table_name>
WHERE <column_name> <condition> <value>)
WHERE <column_name> <condition> <value>;
-- look at the number of rows updated
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/update_statement.htm#CJAGDFFC
SQL Server中
http://msdn.microsoft.com/en-us/library/ms177523.aspx#OtherTables
MySQL中,
mysql> select * from a;
+----+----+----+-------+
| id | c1 | c2 | value |
+----+----+----+-------+
| 1 | 1 | 1 | NULL |
| 2 | 2 | 2 | NULL |
| 3 | 3 | 3 | NULL |
| 4 | 4 | 4 | NULL |
+----+----+----+-------+
mysql> select * from b;
+----+----+-------+
| c1 | c2 | value |
+----+----+-------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
+----+----+-------+
mysql> update a,b set a.value=10*b.value where a.c1=b.c1 and a.c2=b.c2;
Query OK, 0 rows affected (0.15 sec)
Rows matched: 4 Changed: 0 Warnings: 0
mysql> select * from a;
+----+----+----+-------+
| id | c1 | c2 | value |
+----+----+----+-------+
| 1 | 1 | 1 | 10 |
| 2 | 2 | 2 | 20 |
| 3 | 3 | 3 | 30 |
| 4 | 4 | 4 | 40 |
+----+----+----+-------+
4 rows in set (0.00 sec)
【 在 MrK 的大作中提到: 】
: 现有两表格式如下:
: 表a:
: id column1 column2 value
: ...................
☆─────────────────────────────────────☆
MrK (Mr.K) 于 (Thu Sep 8 12:27:53 2011) 提到:
感谢指点 按照update a,b set a.value=10*b.value where a.c1=b.c1 and a.c2=b.c2;参照sql说明成功了 是postgre
这是一条镜像帖。来源:北邮人论坛 / database / #6778同步于 2012/5/20
Database机器人发帖
[合集] 【求助啊 大牛们不辞辛苦点播一下吧 谢谢】关于两个表
doubleKO
2012/5/20镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。