小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

MySQL 多表關(guān)聯(lián)更新

 BigGuo 2010-03-03

SET NAMES gb2312;

CREATE TABLE `o` (
  `id` int(11) default NULL,
  `name` varchar(11) default NULL
) TYPE=MyISAM;

insert into `o` values ('11','數(shù)據(jù)1');
insert into `o` values ('22','數(shù)據(jù)2');
insert into `o` values ('33','數(shù)據(jù)3');
insert into `o` values ('44','數(shù)據(jù)4');


CREATE TABLE `class1` (
  `id` int(11) default NULL,
  `name` varchar(11) default NULL
) TYPE=MyISAM;

insert into `class1` values ('11','數(shù)據(jù)3');
insert into `class1` values ('22','數(shù)據(jù)2');
insert into `class1` values ('33','數(shù)據(jù)4');
insert into `class1` values ('44','數(shù)據(jù)1');

CREATE TABLE `class2` (
  `id` int(11) default NULL,
  `name` varchar(50) default NULL
) TYPE=MyISAM;

insert into `class2` values ('11842542','子數(shù)據(jù)3');
insert into `class2` values ('22543253','子數(shù)據(jù)2');
insert into `class2` values ('33245235','子數(shù)據(jù)4');
insert into `class2` values ('44543254','子數(shù)據(jù)1');


SELECT *
, CONCAT(o.id, RIGHT(B.id, LENGTH(CAST( B.id AS CHAR)) - 2)) value
FROM class1 A, class2 B, o
where A.id = Left(B.id, 2)
and A.name = o.name;

+------+-------+----------+---------+------+-------+----------+
| id   | name  | id       | name    | id   | name  | value    |
+------+-------+----------+---------+------+-------+----------+
|   44 | 數(shù)據(jù)1  | 44543254 | 子數(shù)據(jù)1  |   11 | 數(shù)據(jù)1  | 11543254 |
|   22 | 數(shù)據(jù)2  | 22543253 | 子數(shù)據(jù)2  |   22 | 數(shù)據(jù)2  | 22543253 |
|   11 | 數(shù)據(jù)3  | 11842542 | 子數(shù)據(jù)3  |   33 | 數(shù)據(jù)3  | 33842542 |
|   33 | 數(shù)據(jù)4  | 33245235 | 子數(shù)據(jù)4  |   44 | 數(shù)據(jù)4  | 44245235 |
+------+-------+----------+---------+------+-------+----------+
4 rows in set (0.00 sec)

UPDATE
class2
LEFT JOIN class1  ON class1.id = LEFT(class2.id, 2)
LEFT JOIN o ON class1.name = o.name
SET class2.ID = CONCAT(o.id, RIGHT(class2.id, LENGTH(CAST(class2.id AS CHAR)) - 2)) ;

Query OK, 3 rows affected (0.00 sec)
Rows matched: 4  Changed: 3  Warnings: 0


SELECT * FROM class2;

+----------+---------+
| id       | name    |
+----------+---------+
| 33842542 | 子數(shù)據(jù)3  |
| 22543253 | 子數(shù)據(jù)2  |
| 44245235 | 子數(shù)據(jù)4  |
| 11543254 | 子數(shù)據(jù)1  |
+----------+---------+
4 rows in set (0.00 sec)

DROP TABLE `o`;
DROP TABLE `class1`;
DROP TABLE `class2`;

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多