将数据动态插入到mysql表中

时间:2016-07-11 13:29:56

标签: php mysql

请原谅我缺少正确的问题标题,但我的问题如下

我有一个表格,其数据如下所示:

    mysql> select * from tablex;

+-------+---------+-----+
| id  | post_id   | pid |
+-------+---------+-----+
| 14549 |    7195 |  27 |
| 14551 |    7195 |  34 |
| 14556 |    7195 |   1 |
| 14564 |    7196 |  51 |
| 14566 |    7196 |  11 |
| 14571 |    7196 |  37 |
| 14576 |    7197 |  36 |
| 14578 |    7198 |  11 |
| 14586 |    7199 |  15 |
| 14612 |    7201 |  42 |
+-------+---------+-----+

当我对重复项进行计数时,我得到一个这样的数据结构:

mysql> select count(*), post_id from tablex group by post_id;
+----------+---------+
| count(*) | post_id |
+----------+---------+
|        3 |    7195 |
|        3 |    7196 |
|        1 |    7197 |
|        1 |    7198 |
|        1 |    7199 |
|        1 |    7201 |
+----------+---------+

我正在寻找如何最好地使用php / mysql来操作上面的数据来更新tabley的想法

mysql> select * from tabley order by meta_id desc;
+---------+---------+------------------+---------------+
| meta_id | post_id | meta_key         | meta_value    |
+---------+---------+------------------+---------------+
|    7575 |     7195| multiple         |       3       |
|    7574 |     7195| multiple_0       |      27       |
|    7573 |     7195| multiple_1       |      34       |
|    7572 |     7195| multiple_2       |      1        |
|         |         |                  |               |
+---------+---------+------------------+---------------+

您会注意到post_id 7195发生了3次,因此第一步是将meta_key多次设置为3。

下一步是运行循环并创建密钥0-2并创建元密钥mutiple_0 - multiple_2并插入对应于post_id 7195的值27,34,1,如tablex所示......这是最简单的实现这个目标的方法?

2 个答案:

答案 0 :(得分:1)

您只能使用mysql进行此操作

INSERT INTO tabley (
    post_id,
    meta_key,
    meta_value
) SELECT 
    t1.post_id,
    'multiple' AS multiple,
    COUNT(*)
FROM tablex AS t1 GROUP BY t1.post_id
UNION
SELECT
    t2.post_id,
    REPLACE(
        CONCAT(
            'multiple_',
            @curRow:=CASE
                WHEN @postId = t2.post_id THEN @curRow + 1
                ELSE 0
            END,
            @postId:=t2.post_id
        ),
        t2.post_id,
        ''
    ) AS multiple,
    t2.pid
FROM tablex AS t2
ORDER BY post_id, multiple;

第一部分是INSERT ... SELECT mysql语法。 并且UNION第二次选择。您可以在没有SELECT

的情况下测试INSERT

第二部分是获取具有相似post_id的行的行号。

答案 1 :(得分:1)

这是代码。别忘了更改服务器名,用户名,密码和数据库

$conn = mysqli_connect('localhost', 'root', 'password','database');
  $sql=mysqli_query($conn,"select count(*) as count,post_id from tablex group by post_id");
    while($row=mysqli_fetch_array($sql)){   
        $count[]=$row["count"];
        $postid[]=$row["post_id"];
        }
        foreach (array_combine($postid,$count) as $pid=>$cnt){

            for($i=0;$i<=$cnt;$i++){
                $pstid = $pid;
                if($i==0){
                $multiple = "multiple";
                $meta= $cnt;
                }
                else{
                    $x=$i-1;
                    $multiple = "multiple_".$x;
                    $query=mysqli_query($conn,"select pid from tablex where post_id='$pid'");
                    while($row=mysqli_fetch_array($query)){ 
                    $id[]=$row["pid"];
                    }
                    $meta = $id[$i-1];
                }
$query2=mysqli_query($conn,"INSERT INTO tabley(post_id,meta_key,meta_value) VALUES('$pid','$multiple','$meta')");
            }

        }