mysql结构相同的三张表查询一条记录\将一张表中的数据插入另外一张表

时间:2022-02-10 21:07:01

将一张表中的数据插入另外一张表

1、两张表结构相同

insert   into  表1名称
   select  * from  表2名称

2、两张结构不相同的表

insert   into  表1名称(列名1,列名2,列名3···)
   select  列1,列2,列3 ···  from   表2名称

3、从另一张表获取某几个字段插入另一张表

insert  into  表1名称(列名1,列名2,列名3···)values(数据1,数据2,(select  列名3  from  表2名称 ))

 

三张结构相同的表查询一条记录

 

SELECT
    t.*
FROM
    (
        SELECT
            *
        FROM
            Service_.UserAccount_1
        UNION
            SELECT
                *
            FROM
                Service_.UserAccount_2
            UNION
                SELECT
                    *
                FROM
                    Service_.UserAccount_3
    ) t
WHERE
    t.userId IN (1146190515, 1167527419, 3554447);

 

SELECT
    t.*
FROM
    (
        SELECT
            *
        FROM
            Service_.UserAccount_1
        WHERE
            userId IN (11641901, 11647279, 34547)
        UNION
            SELECT
                *
            FROM
                Service.UserAccount_2
            WHERE
                userId IN (11690415, 11642719, 34447)
            UNION
                SELECT
                    *
                FROM
                    Service.UserAccount_3
                WHERE
                    userId IN (1519015, 10637719, 35447)
    ) t;