0

我有 3 个表并执行了一个数组

$result = mysql_query("Select * from table_3");
while($r[] = mysql_fetch_assoc($result));
echo "<PRE>";
print_r($r);
echo "<PRE>";

输出是:

Array
(
[0] => Array
    (
        [batch_id] => 1
        [seq_id] => 1
        [q_id] => 2046
        [a1] => 0
        [a2] => 1
        [a3] => 2
        [a4] => 3
        [a5] => 4

    )

[1] => Array
    (
         [batch_id] => 1
        [seq_id] => 2
        [q_id] => 2046
        [a1] => 0
        [a2] => 2
        [a3] => 1
        [a4] => 3
        [a5] => 4        
        )
[2] => Array
    (
         [batch_id] => 1
        [seq_id] => 3
        [q_id] => 2046
        [a1] => 2
        [a2] => 0
        [a3] => 2
        [a4] => 2
        [a5] => 1
    )

)

我需要将数组插入到mysql的两个表中。有括号的行是我需要插入的示例

table_1 的结构:

table_1.id 应该是数组的batch_id+seq_id(例如数组[0] 为11)。

table_1.s_id 被硬编码

id   |   s_id    | submit_time
-----------------------------------
2205 |    80     | 1360051287
2214 |    80     | 1360811596
(11) |   (80)    | (1351024912) 
(12) |   (80)    | (1359741512) 
(13) |   (80)    | (1356573212) 

table_2 的结构:

table_2.id 是自动递增的

  id   |   r_id   | q_id   | c_id  | ranking 
-------------------------------------------
  18965|  2205    | 2046   |   a1  |   0
  18966|  2205    | 2046   |   a2  |   2
  18967|  2205    | 2046   |   a3  |   3
  18968|  2205    | 2046   |   a4  |   1    
  18969|  2205    | 2046   |   a5  |   4
  19965|  2214    | 2046   |   a1  |   0
  19966|  2214    | 2046   |   a2  |   1
  19967|  2214    | 2046   |   a3  |   1
  19968|  2214    | 2046   |   a4  |   2    
  19969|  2214    | 2046   |   a5  |   3
(20965)|  (11)    | (2046) | (a1)  |   (0)
(20966)|  (11)    | (2046) | (a2)  |   (1)
(20967)|  (11)    | (2046) | (a3)  |   (2)
(20968)|  (11)    | (2046) | (a4)  |   (3)   
(20969)|  (11)    | (2046) | (a5)  |   (4)
(21965)|  (12)    | (2046) | (a1)  |   (0)
(21966)|  (12)    | (2046) | (a2)  |   (2)
(21967)|  (12)    | (2046) | (a3)  |   (1)
(21968)|  (12)    | (2046) | (a4)  |   (3)   
(21969)|  (12)    | (2046) | (a5)  |   (4)
(22965)|  (13)    | (2046) | (a1)  |   (2)
(22966)|  (13)    | (2046) | (a2)  |   (0)
(22967)|  (13)    | (2046) | (a3)  |   (2)
(22968)|  (13)    | (2046) | (a4)  |   (2)   
(22969)|  (13)    | (2046) | (a5)  |   (1)

有人可以教我如何插入吗?

4

1 回答 1

0

我无法理解您如何为 table2 生成值。但是对于 table_1,您为什么要将数据带入 PHP?您应该为此使用以下 sql 命令:

INSERT INTO table_1(id) SELECT CONCAT(batch_id, seq_id) FROM table_3
于 2013-04-26T06:35:25.700 回答