如何为每条记录运行SQL查询

时间:2014-04-18 15:14:46

标签: php mysql

我想根据以前运行的查询的结果向DB插入一些数据。

// ------DEFINE ATTACHMENT--------
$sqlText['attachment']="SELECT
                            issues.`subject`,
                            issue_relations.issue_from_id,
                            issue_relations.issue_to_id,
                            issues.id,
                            attachments.filename,
                            attachments.digest,
                            attachments.id
                        FROM
                            issues
                        INNER JOIN issue_relations ON issues.id = issue_relations.issue_to_id
                        OR issues.id = issue_relations.issue_from_id
                        INNER JOIN attachments ON issue_relations.issue_to_id = attachments.container_id
                        OR issue_relations.issue_from_id = attachments.container_id
                        WHERE
                            issue_from_id = $myPage
                        OR issue_to_id = $myPage
                        GROUP BY
                            digest";
    $sqlQuery_MainMenu = mysql_db_query($db, $sqlText['attachment'], $baglanti) or die("error");
    while($mm_Content=mysql_fetch_array($sqlQuery_MainMenu)){
// ------MAKE ATTACHMENT--------    
            $myValue=$mm_Content['id'];
        $sqlText['attach']="INSERT INTO attachments (
            container_id,
            container_type,
            filename,
            disk_filename,
            filesize,
            content_type,
            digest,
            downloads,
            author_id,
            created_on,
            description,
            disk_directory
        ) SELECT
            $value,
            container_type,
            filename,
            disk_filename,
            filesize,
            content_type,
            digest,
            downloads,
            author_id,
            created_on,
            description,
            disk_directory
        FROM
            attachments
        WHERE
            attachments.id = $myValue";
    $sqlQuery_MainMenu = mysql_db_query($db, $sqlText['attach'], $baglanti) or die("error");        
}

现在,交易$mm_Content['id'];帮助我找到需要添加的记录数量。 并且MAKE ATTACHMENT部分将数据插入DB。除非只插入一条记录,否则一切正常。 但$mm_Content['id'];大部分时间都会返回多条记录。在这种情况下,上面的代码只获得最新的代码。但是,当我在mysql中运行查询时,它会显示比预期更多的记录。

为了处理我试图制作一个$mm_Content['id'];数组,但它不起作用(我无法让它运行)。它也只带来了一条最新的记录。

我想相应地运行MAKE ATTACHMENT PART从$mm_Content['id'];

返回的记录

我的意思是,如果有四条记录,则为每条$mm_Content['id'];记录运行插入次数。

我该怎么做? 期待听到您宝贵的帮助。 问候。

1 个答案:

答案 0 :(得分:2)

您如何看待子查询?

$sqlText['attach']="INSERT INTO attachments (
                container_id,
                container_type,
                filename,
                disk_filename,
                filesize,
                content_type,
                digest,
                downloads,
                author_id,
                created_on,
                description,
                disk_directory
            ) SELECT
                $value,
                container_type,
                filename,
                disk_filename,
                filesize,
                content_type,
                digest,
                downloads,
                author_id,
                created_on,
                description,
                disk_directory
            FROM
                attachments
            WHERE
                attachments.id in
    (
    SELECT   attachments.id
                            FROM
                                issues
                            INNER JOIN issue_relations ON issues.id = issue_relations.issue_to_id
                            OR issues.id = issue_relations.issue_from_id
                            INNER JOIN attachments ON issue_relations.issue_to_id = attachments.container_id
                            OR issue_relations.issue_from_id = attachments.container_id
                            WHERE
                                issue_from_id = $myPage
                            OR issue_to_id = $myPage
                            GROUP BY
                                digest
    )";
    $sqlQuery_MainMenu = mysql_db_query($db, $sqlText['attach'], $baglanti) or die("error");