简体   繁体   中英

How can i make a sql query to run for each record

I want to insert some data to DB according to results of a previous running query.

// ------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");        
}

Now here is the deal $mm_Content['id']; helps me to find how many records needs to be added. And MAKE ATTACHMENT part inserts data to DB. Everything works fine unless if there is only one record to insert. But $mm_Content['id']; returns with multiple records most of the time. In that case the code that above gets latest one only. However when i run query inside mysql it shows more records than one as expected.

To deal with i tried to make an array of $mm_Content['id']; but it did not work (i could not make it run probably). It also brought only one record that is latest.

I want to run MAKE ATTACHMENT PART accordingly the record that returns from $mm_Content['id'];

I mean if there are four records then run insert into for times for each $mm_Content['id']; record.

How can i do that. Looking forward to hear your precious helps. Regards.

What do you think about subquery?

$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"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM