I'd like Wordpress query to possess a foreach inside it.

$aid=0;
    foreach ($QA as $key => $value) {
    AND wp_postmeta.post_id IN (
        SELECT wp_postmeta.post_id
        FROM wp_postmeta, wp_posts
        WHERE wp_posts.ID = wp_postmeta.post_id
        AND wp_posts.post_status = 'publish'
        AND wp_posts.post_type = 'post'
        AND wp_postmeta.meta_key = '$key'
        AND wp_postmeta.meta_value = '$value'
    )
    $aid++;
}

But it is no longer working this way, so I am requesting help -

The Way I can put foreach in sql query?

Full illustration of code below.

<?php
 function query_products($QA) {

global $wpdb;
global $post;
global $pageposts;

$querystr = "

SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'

$aid=0;
    foreach ($QA as $key => $value) {
    AND wp_postmeta.post_id IN (
        SELECT wp_postmeta.post_id
        FROM wp_postmeta, wp_posts
        WHERE wp_posts.ID = wp_postmeta.post_id
        AND wp_posts.post_status = 'publish'
        AND wp_posts.post_type = 'post'
        AND wp_postmeta.meta_key = '$key'
        AND wp_postmeta.meta_value = '$value'
    )
    $aid++;
}

ORDER BY wp_postmeta.meta_value ASC 
    ";

$pageposts = $wpdb->get_results($querystr, OBJECT);

}

$QA = array (
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
);
query_products($QA);
?>
$querystr = "
SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'
";

$aid=0;
    foreach ($QA as $key => $value) {
       $querystr .= "
    AND wp_postmeta.post_id IN (
        SELECT wp_postmeta.post_id
        FROM wp_postmeta, wp_posts
        WHERE wp_posts.ID = wp_postmeta.post_id
        AND wp_posts.post_status = 'publish'
        AND wp_posts.post_type = 'post'
        AND wp_postmeta.meta_key = '$key'
        AND wp_postmeta.meta_value = '$value'";
    )
    $aid++;
}
$querystr .=" ORDER BY wp_postmeta.meta_value ASC";

Not understanding exactly what the query should really accomplish, I'm able to only suggest something similar to this:

<?php

$querystr = "
SELECT wp_posts.*, wp_postmeta.*
FROM wp_postmeta, wp_posts
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_postmeta.meta_key = 'country'
AND wp_postmeta.meta_value = 'Denmark'
AND wp_postmeta.post_id IN (
    SELECT wp_postmeta.post_id
    FROM wp_postmeta, wp_posts
    WHERE wp_posts.ID = wp_postmeta.post_id
    AND wp_posts.post_status = 'publish'
    AND wp_posts.post_type = 'post'
";
if (count($QA) >= 1) {
    $querystr .= " AND ( ";
    foreach($QA as $key => $value) {
        $querystr .= "(wp_postmeta.meta_key = '{$key}' AND wp_postmeta.meta_value = '{$value}') OR ";
    }
    // get rid of the last "OR" in the string
    $querystr = substr($querystr,0,strrpos($querystr,"OR"));
    $querystr .= " ) ";
}
$querystr .= " ORDER BY wp_postmeta.meta_value ASC ";

?>

Note I made use of "OR" above because you may never have any results when checking just one area for multiple values utilizing an "AND" clause.