SQL - 将另一个表的值插入多个列并将它们嵌入预定义的值中

时间:2017-12-31 12:28:52

标签: mysql sql

我有两个表,想要在表2中插入表1的值:

Table 1: tx_abc_domain_model_abc
Columns: crdate, uid, xyz

Table 2: tx_realurl_urldata
Columns: crdate, page_id, rootpage_id, original_url, speaking_url, request_variables, expire

...表1的值......

crdate_table1
uid_table1
xyz_table1

...应作为子部件嵌入预定义的值中,如表2所示:

SET VALUES (crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_lei_lei%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0);

所以结果应该是看起来像

(crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0),
(crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0),
(crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0),
(crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0),
(crdate_table1, 43, 1, 'id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'abc/xyz_table1/', '{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 0),
...

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你可以这样做:

insert into table2 (crdate, page_id, rootpage_id, original_url, speaking_url, request_variables, expire)
    select t1.crdate_table1, 43, 1, 
           replace('id=43&tx_abc_abc%5Baction%5D=show&tx_abc_abc%5Bcontroller%5D=Abc&tx_abc_abc%5Brecord%5D=uid_table1', 'uid_table1', t1.uid),
           replace('abc/xyz_table1/', 'xyz_table1', t1.xyz),
           replace('{"id":"43","tx_abc_abc[record]":"uid_table1","tx_abc_abc[action]":"show","tx_abc_abc[controller]":"Abc"}', 'uid_table1', t1.uid),
            0
    from table1 t1;

请注意使用replace()插入另一个表中的值。