简体   繁体   English

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

[英]Query a MySql Database Using The Results of Another Query

My PHP code is below: 我的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

As you can see, I'm using the results of $query1 to query the condo table ($query2). 如您所见,我正在使用$ query1的结果查询公寓表($ query2)。 I don't want to do this anymore. 我不想再这样做了。 Could you show me how to combine both queries into a single query & achieve the same result? 您能告诉我如何将两个查询合并为一个查询并获得相同的结果吗?

Thanks a lot. 非常感谢。

Latest Update Okay this is what I've done so far. 最新更新好的,这是我到目前为止所做的。

<?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>';

            }
    }

    ?>

From this code, what is supposed to happen is a keyword stored in test_log is used to query the location column of the condo table. 从此代码中,应该发生的是存储在test_log中的关键字用于查询condo表的location列。 The results of this query is then emailed to the email address that corresponds with the keyword. 然后,该查询的结果将通过电子邮件发送到与关键字相对应的电子邮件地址。 The email address is stored in the email column of test_log . 电子邮件地址存储在test_log电子邮件列中。

I've not incorporated the emailing script yet. 我还没有合并电子邮件脚本。 I'm focusing on displaying the query results first to check whether the right message is supposed to go to the right email or not. 我专注于首先显示查询结果,以检查是否应该将正确的消息发送到正确的电子邮件。

I don't think it is looping properly but I can't explain why either... 我认为循环不正常,但我也无法解释为什么...

Latest UPDATE Okay this is my new query which does not work when run directly on PHP MyAdmin. 最新更新好的,这是我的新查询,当直接在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

The message that I get on PHP MyAdmin is: 我在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

How do I get this working? 我该如何工作? Thanks for your help. 谢谢你的帮助。

Use a JOIN : 使用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

I won't get into the specifics of the query but I will get you started with what I believe is the first step in the right direction. 我不会深入查询的细节,但是我会让您从正确的方向迈出第一步。 What you are trying to do should be possible with a nested query: 嵌套查询应该可以实现您想做的事情:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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