当前位置:首页 > 开发 > 数据库 > 正文

一个简单的Update语句

发表于: 2009-08-21   作者:crabdave   来源:转载   浏览:
摘要: 一个简单的Update语句 mysql> create table aa(name varchar(100),grade int(5));Query OK, 0 rows affected (0.09 sec) mysql> create table bb(name varchar(100),age int(5));Query OK, 0 rows affected (0.14 s

一个简单的Update语句



mysql> create table aa(name varchar(100),grade int(5));
Query OK, 0 rows affected (0.09 sec)

mysql> create table bb(name varchar(100),age int(5));
Query OK, 0 rows affected (0.14 sec)

mysql> insert into aa values('AAA',80);
Query OK, 1 row affected (0.05 sec)

mysql> insert into aa values('BBB',60);
Query OK, 1 row affected (0.06 sec)

mysql> insert into aa values('CCC',60);
Query OK, 1 row affected (0.03 sec)

mysql> insert into bb values('CCC',26);
Query OK, 1 row affected (0.05 sec)

mysql> insert into bb values('BBB',24);
Query OK, 1 row affected (0.03 sec)

mysql> insert into bb values('AAA',26);
Query OK, 1 row affected (0.03 sec)

mysql> select * from aa;
+------+-------+
| name | grade |
+------+-------+
| AAA  |    80 |
| BBB  |    60 |
| CCC  |    60 |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from bb;
+------+------+
| name | age  |
+------+------+
| CCC  |   26 |
| BBB  |   24 |
| AAA  |   26 |
+------+------+
3 rows in set (0.00 sec)

--用aa表的grade更新bb表的age

mysql> update bb b set age=(select grade from aa a where a.name=b.name);
Query OK, 3 rows affected (0.03 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from bb;
+------+------+
| name | age  |
+------+------+
| CCC  |   60 |
| BBB  |   60 |
| AAA  |   80 |
+------+------+
3 rows in set (0.00 sec)

mysql>

一个简单的Update语句

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号