PHP MySQL - INSERT INTO SELECT顺序

时间:2016-02-23 10:59:56

标签: php mysql insert

我需要一些帮助来做php脚本。好吧,我需要在另一个名为runtable的表中填充一个新表 PIVOTrun 。当我知道列名时,我可以毫无问题地做到这一点。我的目标是生成一个通用脚本,在顺序扫描中将此插入到select中。我的大问题是,我没有设法修改列名并在我的php脚本中生成正确的通用SQL查询。

我的PIVOTrun表具有以下结构:

CREATE TABLE PIVOTrun (`ID` varchar(10), `Reference`varchar(6), `Variant` varchar(6), `ComputeVariant` varchar(20),`HGVSvar`varchar(50), `ProtDesc` varchar(50), `a combined %` decimal(5,2),`a combined of` int (4), `a forward %` decimal(5,2), `a forward of` int(4),`a reverse %` decimal(5,2), `a reverse of` int(4),
`b combined %` decimal(5,2),`b combined of` int (4), `b forward %` decimal(5,2), `b forward of` int(4),`b reverse %` decimal(5,2), `b reverse of` int(4)                      
)

以下是我的禅宗样本:

Reference   Variant     3SA46FEDa combined %    3SA46FEDa combined of   3SA46FEDa forward %     3SA46FEDa forward of    3SA46FEDa reverse %     3SA46FEDa reverse of    3SA46FEDb combined %    3SA46FEDb combined of   3SA46FEDb forward %     3SA46FEDb forward of    3SA46FEDb reverse %     3SA46FEDb reverse of    34S06MILa combined %    34S06MILa combined of   34S06MILa forward %     34S06MILa forward of    34S06MILa reverse %     34S06MILa reverse of    34S06MILb combined %    34S06MILb combined of   34S06MILb forward %     34S06MILb forward of    34S06MILb reverse %     34S06MILb reverse of    
ALKe23          38:T/C          0.00                    408                     0.00                    166                     0.00                     242                     0.00                   169                     0.00                     44                     0.00                    125                     0.00                     2355                   0.00                    991                     0.00                    1364                    0.00                    1525                    0.00                    555                     0.00                    970
ALKe23          168:G/A     35.78               408                     31.33               166                     38.84                    242                     53.25                  169                     47.73                    44                     55.20                   125                     0.00                     2355                   0.00                    991                     0.00                    1364                    0.00                    1525                    0.00                    555                     0.00                    970

为了填充我的PIVOTrun表,我这样做:

insert into PIVOTrun SELECT '3SA46FED',`Reference`,`Variant`,`ComputeVariant`,`HGVSvar`,`ProtDesc`,`3SA46FEDa combined %`, `3SA46FEDa combined of`, `3SA46FEDa forward %`, `3SA46FEDa forward of`, `3SA46FEDa reverse %`, `3SA46FEDa reverse of`, `3SA46FEDb combined %`, `3SA46FEDb combined of`, `3SA46FEDb forward %`, `3SA46FEDb forward of`, `3SA46FEDb reverse %`, `3SA46FEDb reverse of` FROM run09022016

insert into PIVOTrun SELECT '34S06MIL',`Reference`,`Variant`,`ComputeVariant`,`HGVSvar`,`ProtDesc`,`34S06MILa combined %`, `34S06MILa combined of`, `34S06MILa forward %`, `34S06MILa forward of`, `34S06MILa reverse %`, `34S06MILa reverse of`, `34S06MILb combined %`, `34S06MILb combined of`, `34S06MILb forward %`, `34S06MILb forward of`, `34S06MILb reverse %`, `34S06MILb reverse of` FROM run09022016

我需要找到一种以顺序和通用的方式生成这些查询的方法。 我在PHP中尝试的是:

<?php
require_once "config.php";

//Get column names
$resultShowCol = mysqli_query($conn,"SHOW COLUMNS FROM run09022016");


if (mysqli_num_rows($resultShowCol) > 0) {
    while ($row = mysqli_fetch_assoc($resultShowCol)) {
        $columns[] = $row['Field'];

    }
    foreach($columns as $cell)
        $out=substr($cell,0,strpos($cell, "a combined of"));
}

$sql = "INSERT INTO PIVOTrun SELECT '.$out.' ";
foreach($columns as $ind=>$val){


        $sql .= $val.",";


}
$sql = rtrim($sql,",");
$sql .= ' FROM run09022016';
echo $sql;
?>

但我没有设法对感兴趣的行进行子字符串处理,并且我无法按顺序生成查询。我有我的查询中的所有字段。有谁知道怎么做?希望有人可以帮助我。 很多人都关注我的帖子。

0 个答案:

没有答案