Home >Database >Mysql Tutorial >[转]mysql从一个表字段赋值给另一个表字段 搜集

[转]mysql从一个表字段赋值给另一个表字段 搜集

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 16:16:031310browse

[转]mysql从一个表字段赋值给另一个表字段 收集 方法一: mysql从一个表字段赋值给另一个表字段,在线等待。 table1 ? 中 ? 有字段 ? id,uid,name table2 ? 中 ? 有字段 ? id,name table1中的uid和table2中的id是一一对应的。 我如何把table2中的name一一对应

[转]mysql从一个表字段赋值给另一个表字段 收集

方法一:

mysql从一个表字段赋值给另一个表字段,在线等待。
table1 ? 中 ? 有字段 ? id,uid,name
table2 ? 中 ? 有字段 ? id,name

table1中的uid和table2中的id是一一对应的。
我如何把table2中的name一一对应赋值给table1中name
我的环境是MYSQL4.0.18nt+PHP4

------解决方法--------------------
INSERT INTO table1 (uid, name)
SELECT id AS uid,
name
FROM table2;
------解决方法--------------------
update table1,table2 set table1.name = table2.name where table1.uid = table2.id;

?

方法二:

网上的题目写的:
有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value
这道题的SQL语句怎么写?
update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);
但是如果没有ID字段 这样更新是否可以?

update b set b.value=(select a.value from a where a.key=b.key) where b.key in(select b.key from b,a where b.key=a.key);





------解决方法--------------------
没什么问题,但习惯都写成 exists,不用in
------解决方法--------------------
可以的,
还可以用merge,更简洁。

SQL code
merge into b using a on (b.key=a.key)
when matched then
  update
  set b.value=a.value; 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn