簡體   English   中英

如何基於另一個查詢結果集執行MySQLi查詢?

[英]How to perform MySQLi query based off of another query result set?

我知道有一種方法可以使用JOIN組合這三個查詢,但是我無法解決這個問題。

我正在做的是首先檢索MessageIDSubscriberID 根據這些結果,我收集消息的數據和訂戶的數據:

$queued = $db->select("SELECT MessageID, SubscriberID FROM mailing_queue WHERE Paused = 0 ORDER BY StartDate ASC LIMIT 500");

if (!empty($queued)) { // if $queued exists

    $i = 0;

    foreach ($queued as $key => $value) {           

        $msg = $db->selectRow("SELECT Subject, Body FROM mailing_messages WHERE ID = '$value[MessageID]' LIMIT 1"); // Returns single row

        if ($msg) { // If $msg exists

            $to = $db->selectRow("SELECT Email, FirstName, LastName, History FROM mailing_subscribers WHERE ID = '$value[SubscriberID]' LIMIT 1"); // Returns single row

            if ($to) { // If $to exists

                // Send email & do other stuff

            }
        }
    }
}

基本上,我試圖在一個查詢中盡可能獲取SubjectBodyEmailFirstNameLastNameHistory

我知道必須有一種更有效的方法。

因此,此查詢應該在代碼的if($to)段中為您提供一些建議。 您應該在該行中擁有所有可用的東西。

$queued = $db->select(
"SELECT
 mailing_queue.MessageID,
 mailing_queue.SubscriberID,
 mailing_messages.Subject, 
 mailing_messages.Body,
 mailing_subscribers.Email, 
 mailing_subscribers.FirstName, 
 mailing_subscribers.LastName, 
 mailing_subscribers.History 
 FROM mailing_queue
 INNER JOIN mailing_messages ON mailing_messages.ID = mailing_queue.MessageID
 INNER JOIN mailing_subscribers ON mailing_subscribers.ID = mailing_queue.SubscriberID
 WHERE mailing_queue.Paused = 0 
 ORDER BY mailing_queue.StartDate 
 ASC LIMIT 500");

foreach($queued as $key=>$to){
    //whatever your send your email code is, demonstration:
    echo $to['Subject'];
    echo $to['SubscriberID'];
    echo $to['FirstName'];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM