使用另一个查询的结果查询MySql数据库

时间:2015-04-14 14:17:00

标签: php mysql

我的PHP代码如下:

    $query1=("SELECT * FROM test_log WHERE '$mktime' < expiry ORDER BY id DESC"); 
        $result1=mysql_query($query1); 
        $num1=mysql_num_rows($result1);


    while ($row1 = mysql_fetch_array($result1))
        { 
        $id = $row1["id"];
        $keyword = $row1["keyword"];
        $sale = $row1["sale_rent"];
        $agents = $row1["e_num"];
        $email = $row1["email"];
        $cc_email = $row1["cc_email"];
        $expiry = $row1["expiry"];

        $query2= ("SELECT * FROM condo WHERE (location LIKE '%{$row1['keyword']}%' AND sale_rent LIKE '%{$row1['sale_rent']}%' AND e_num LIKE '{$row1['e_num']}') AND (date >= '$sendate') AND TRIM(IFNULL(`phone_1`,'')) <> '' ORDER BY sale_rent, location");
        $result2=mysql_query($query2);    
    $num2=mysql_num_rows($result2);
//the code continues

如您所见,我使用$ query1的结果查询 condo 表($ query2)。我不想再这样做了。你能告诉我如何将两个查询合并到一个查询中吗?实现相同的结果?

非常感谢。

最新更新 好的,这是我到目前为止所做的。

<?php   
include("/path/to/my/website.com/public_html/db_connect.php");

$mktime = date('Y-m-d');
$sendate = date('Y-m-d', strtotime('1 days ago'));

$query1=("SELECT * FROM test_log AS t JOIN condo AS c ON c.location LIKE CONCAT('%', t.keyword, '%') AND c.sale_rent LIKE CONCAT('%', t.sale_rent, '%') AND c.e_num LIKE CONCAT('%', t.e_num, '%') WHERE t.expiry > '$mktime' AND c.date > '$sendate' AND TRIM(IFNULL(c.phone_1, '')) != '' ORDER BY t.id, c.sale_rent, c.location"); 
$result1=mysql_query($query1); 
$num1=mysql_num_rows($result1);

$subject = "Latest Classified Ads - Condo";

$message_tmp  = "<div align='center'>";
$message_tmp .= "<TABLE cellSpacing=0 cellPadding=0 width=100% border=0 id='table1'>";
$message_tmp .= "<TR>";
$message_tmp .= "<TD width=100% height=20 bgcolor='#E5E5E5' align='left' valign='top'>
<p align='center'><b><font size='3'>Comprehensive Real Estate Agency Tools & Property Valuation Services at <a href=\"http://www.cashbillion.com/free-email-alerts\">http://www.cashbillion.com</a></font></b></TD>";
$message_tmp .= "</TR>";
$message_tmp .= "<TR>";
$message_tmp .= "<TD width=100% height=20 bgcolor='#EFEFEF' align='left' valign='top'>
    ~ Classified Ads<br>
    ~ Facebook Promotions<br>
    ~ Public Records Search<br>
    ~ Demographic Statistics<br>
    ~ Property Market Values<br>
    ~ Property Owners Listings<br>
    ~ Real Estate Price Trend Analysis<br>
</p></TD>";
$message_tmp .= "</TR>";
$message_tmp .= "<TR>";
$message_tmp .= "</TR>";
$message_tmp .= "</TABLE>";
$message_tmp .= "</div>";
$message_tmp .= "<h4>List of Latest Classified Ads - Condo</h4>"; 
$message_tmp .= "<table bgcolor='black' cellspacing='1' width='100%'><tr bgcolor='white' align='center'>"; 
$message_tmp .= "<td><b>Date</b></td>";
$message_tmp .= "<td><b>Location</b></td>"; 
$message_tmp .= "<td><b>Property Type</b></td>";
$message_tmp .= "<td><b>Tenure</b></td>";
$message_tmp .= "<td><b>Status</b></td>";
$message_tmp .= "<td><b>Sale / Rent</b></td>";
$message_tmp .= "<td><b>Size</b></td>";
$message_tmp .= "<td><b>Price</b></td>";
$message_tmp .= "<td><b>Price Psf</b></td>";
$message_tmp .= "<td><b>Rooms</b></td>";
$message_tmp .= "<td><b>Baths</b></td>";
$message_tmp .= "<td><b>Renovations</b></td>";
$message_tmp .= "<td><b>Furnishings</b></td>";
$message_tmp .= "<td><b>Phone</b></td>";
$message_tmp .= "<td><b>Advertiser</b></td>";
$message_tmp .= "<td><b>E Number</b></td>";
$message_tmp .= "</tr>"; 
$message_tmp .= "[new_rows]"; 
$message_tmp .= "</table>";
$message_tmp .= "<br>To unsubscribe, go to <a href=\"http://www.mywebsite.com/unsubscribe-from-email-alerts/\"><strong>http://www.mywebsite.com/unsubscribe-from-email-alerts/</a></strong>";
$message_tmp .= "<br>Try our free services at <a href=\"http://www.mywebsite.com/free-email-alerts/\"><strong>http://www.mywebsite.com/free-email-alerts/</a></strong> your property market information provider.";

while ($row1 = mysql_fetch_array($result1))
    { 
$new_rows .= "<tr bgcolor='white' align='left'>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["date"]."</td>";
$new_rows .= "<td align=left><font face=arial size=1.5>".$row1["location"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["property_type"]."</td>";
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["tenure"]."</td>";
$new_rows .= "<td align=left><font face=arial size=1.5>".$row1["status"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["sale_rent"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["size"]."</td>";
$new_rows .= "<td align=left><font face=arial size=1.5>".$row1["price"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["price_psf"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["rooms"]."</td>";
$new_rows .= "<td align=left><font face=arial size=1.5>".$row1["baths"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["renovations"]."</td>";
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["furnishings"]."</td>";
$new_rows .= "<td align=left><font face=arial size=1.5>".$row1["phone_1"]."</td>"; 
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["advertiser"]."</td>";  
$new_rows .= "<td align=center><font face=arial size=1.5>".$row1["e_num"]."</td>";
$new_rows .= "</tr>";

    $email = $row1["email"];
    $cc_email = $row1["cc_email"];
    $expiry = $row1["expiry"];
    $keyword = $row1["keyword"];

    if (!empty($new_rows))
            {
            $message = str_replace("[new_rows]",$new_rows,$message_tmp);
            $message .= "<br>Your notification will expire on <font color='#FF0000'><strong>$expiry</strong></font>";
            $message .= "<br>Your requested keyword is <font color='#FF0000'><strong>$keyword</strong></font><br><br>";

    echo $email;
    echo ',';
    echo $cc_email;
    echo ',';
    echo $keyword;
    echo '<br>';
    echo $subject;
    echo '<br>';
    echo $message;
    echo '<br>';
    echo $new_rows;
    echo '<br><br>';

            }
    }

    ?>

从此代码中可以看出,存储在 test_log 中的关键字用于查询公寓表的位置列。然后,将此查询的结果通过电子邮件发送到与关键字对应的电子邮件地址。电子邮件地址存储在 test_log 电子邮件列中。

我还没有收录电子邮件脚本。我首先专注于显示查询结果,以检查正确的邮件是否应该转到正确的电子邮件。

我不认为它正在循环,但我无法解释为什么......

最新更新 好的,这是我的新查询,直接在PHP MyAdmin上运行时无效。

SELECT condo.location, condo.sale_rent, condo.e_num, condo.date, condo.phone_1 innerQuery.* FROM condo JOIN (SELECT * FROM test_log WHERE '2015-04-15' < expiry ORDER BY id DESC) AS innerQuery WHERE condo.location = innerQuery.keyword AND condo.sale_rent = innerQuery.sale_rent AND condo.e_num = innerQuery.e_num AND condo.date >= '2015-04-14' AND TRIM(IFNULL(`condo.phone_1`,'')) <> '' ORDER BY condo.sale_rent, condo.location

我在PHP MyAdmin上收到的消息是:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.* FROM condo JOIN (SELECT * FROM test_log WHERE '2015-04-15' < expiry ORDER BY ' at line 1

我如何让这个工作?谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

使用JOIN

SELECT *
FROM test_log AS t
JOIN condo AS c 
ON c.location LIKE CONCAT('%', t.keyword, '%') 
    AND c.sale_rent LIKE CONCAT('%', t.sale_rent, '%')
    AND c.e_num LIKE CONCAT('%', t.e_num, '%')
WHERE t.expiry > '$mktime'
    AND c.date > '$sendate'
    AND TRIM(IFNULL(c.phone_1, '')) != ''
ORDER BY t.id, c.sale_rent, c.location

答案 1 :(得分:0)

我不会深入了解查询的具体细节,但我会从一开始就认识到我认为这是朝着正确方向迈出的第一步。您应该尝试使用嵌套查询:

SELECT condo.FieldA, innerQuery.* FROM condo JOIN (SELECT * FROM test_log WHERE '$mktime' < expiry ORDER BY id DESC) AS innerQuery WHERE condo.FieldA = innerQuery.SomeField
相关问题