0

我有 3 个包含几个字段的 mysql 表。请参考以下

--------  --------  --------
table 1 | table 2 | table 3
--------  --------  --------
a_id    | b_id    | email
                  | a_id
name    | status  | b_id
email_1 | date    | name
phone   | ref     | phone
address | email_2 | address
state   | from    | state
                  | status
                  | date
                  | ref
                  | from

email_1和email_2完全相同

我需要用所有 table1 和 table2 数据填充 table3 字段。但我需要根据电子邮件将它们存储在一行中。所以它们看起来像下面这样:

=================================================================================
                                   table 3
=================================================================================
email | a_id | b_id | name | phone | address | state | status | date | ref | from
------+------+------+------+-------+---------+-------+--------+------+-----+-----
a@x.co|  9   | 112  | John |  999  | xxxx    |   NY  |    0   | 15Jue| dave|  y
------+------+------+------+-------+---------+-------+--------+------+-----+-----
b@x.co|  6   | 338  | Sue  |  909  | xxxx    |   NY  |    1   | 12Jue| kell|  z
------+------+------+------+-------+---------+-------+--------+------+-----+-----
c@x.co|  3   | 152  | John |  679  | xxxx    |   NY  |    1   | 10Jue| lois|  g
------+------+------+------+-------+---------+-------+--------+------+-----+-----
d@x.co|  8   | 145  | John |  599  | xxxx    |   NY  |    0   | 8Jue | sue |  f

我不知道该怎么做。我正在使用核心 php,mysql。请问有什么帮助吗?

谢谢!

4

2 回答 2

0
insert into table_3(`email`,`a_id`,`b_id`,`name`,`phone`,`address`,`state`,`status`,`date`,`ref`,`from`)
select `email_1`,`a_id`,`b_id`,`name`,`phone`,`address`,`state`,`status`,`date`,`ref`,`from`
from table_1,table_2
where email_1=email_2;
于 2013-06-15T09:15:52.227 回答
0

我会为mysql使用这样的东西:

insert into table_3
(
    `email`,
    `a_id`,
    `b_id`,
    `name`,
    `phone`,
    `address`,
    `state`,
    `status`,
    `date`,
    `ref`,
    `from`
)
select
    `a`.`email_1`,
    `a`.`a_id`,
    `b`.`b_id`,
    `a`.`name`,
    `a`.`phone`,
    `a`.`address`,
    `a`.`state`,
    `b`.`status`,
    `b`.`date`,
    `b`.`ref`,
    `b`.`from`
from 
    table_1 as `a`
    inner join
    table_2 as `b` on `a`.`email_1` = `b`.`email_2`

但是您可能应该阅读MySQL 插入语法以了解它的功能以及加入数据:)

于 2013-06-15T09:17:27.187 回答