从匹配条件的多个表中获取数据库中的数据

时间:2014-09-12 06:17:18

标签: wp-query mysql

我想从数据库中获取数据。我的数据库中有三个表:listing_master_residentiallisting_master_condolisting_master_commercial。所有表中都有一个主键Ml_num。我想从一个匹配mls数字表的表中搜索数据。

if (isset($_POST['search'])) {
    $mls=$_POST['mls_number'];
    $sql = "SELECT * FROM listing_master_residential,
        listing_master_condo, 
        listing_master_commercial  
        INNER JOIN listing_master_residential AS res ON res.Ml_num=Ml_num
        INNER JOIN listing_master_condo AS con ON con.Ml_num=Ml_num
        INNER JOIN listing_master_commercial AS com ON com.Ml_num=Ml_num
        WHERE Ml_num='$mls'";
    $result = $wpdb->get_results($sql) or die(mysql_error());
    foreach ( $result as $row) {
        echo $row->Lot_code."<br/>";
        echo $row->Ml_num."<br/>";
        echo $row->Acres;
        echo $row->Addr."<br/>";
        echo $row->Bath_tot;
        echo $row->Br;
        echo $row->Br_plus;
    }
}

有了上述内容,我收到了一个错误:

  

where子句中的列'Ml_num'是不明确的

1 个答案:

答案 0 :(得分:0)

因为Ml_num列在所有表格中,您需要使用完全限定名称来解决它,例如con.Ml_num

我还注意到你加入所有表格两次,一次使用隐式连接,一次使用ANSI连接 隐式连接将导致交叉连接,因为where子句中没有连接条件。

相关问题