1

数据库 1

Table A
+-----+--------+---------------+
|  ID |  Price | Date          |
+-----+--------+---------------+
|  1  |    100 |  2013.07.07   |
+-----+--------+---------------+

Table B
+------+--------+-------------+-------+
|  ID  | Name   | Description | Stock |
+------+--------+-------------+-------+
| A-ID |  Name1 |  Good       |   100 |
+------+--------+-------------+-------+

从这些到这些

Table C
+------+--------+---------------+--------+
|  ID  | Name   | Description   | Date   |
+------+--------+---------------+--------+
| A-ID | B-Name | B-Description | A-Date |
+------+--------+---------------+--------+

Table D
+-------+------+-------+----------+
|  D_ID | ID   | Meta  | MetaValue|
+-------+------+-------+----------+
|  1    | A-ID | price | A-Price  |
+-------+------+-------+----------+
|  2    | A-ID | stock | B-Stock  |
+-------+------+-------+----------+

如何用一个 PhP 代码填充 A 和 B 的 C 和 D 表?xS 在表 A 和表 B 中,我有 15.000~ 行 C 需要为每个项目制作 1 行 D 需要为每个项目制作 30 行具有不同的值(在表 A 和表 B 中)

我以这种方式填写“表 C”

INSERT INTO DB1.C (Name, Description) 
SELECT b.Products_Name, b.Description
FROM DB1.B AS b

UPDATE DB1.C c, DB1.A a
SET c.Date=a.Date
WHERE c.ID=a.ID'
4

2 回答 2

0

插入 TableC

insert into TableCselect b.id,b.name,b.description,a.date 
from TableA a, TableB b 
where a.id = b.id

插入 TableD

INSERT INTO TableD (ID,Meta, MetaValue) 
select id,'price',Price from TableA
Union
select id,'stock',Stock from TableB
于 2013-07-24T13:08:04.333 回答
0

灌装表C:

INSERT INTO C (ID, Name, Description, Date)
SELECT A.ID, B.Name, B.Description, A.Date
FROM A INNER JOIN B ON A.ID = B.ID;

填充表 D 将是两个语句:

INSERT INTO D (ID, Meta, MetaValue) /*assuming here, that D_ID is auto_increment...*/
SELECT A.ID, 'price', Price
FROM A
UNION ALL
SELECT A.ID, 'stock', B.Stock
FROM A INNER JOIN B ON A.ID = B.ID;
于 2013-07-24T13:11:43.813 回答