INNER JOIN具有相同列名的多个表

时间:2018-05-29 08:20:44

标签: php mysql inner-join

我正在尝试使用此查询从表中获取数据:

$query = "SELECT wplt_wpl_properties.mls_id, 
            wplt_wpl_properties.property_type,
            wplt_wpl_properties.location1_name,
            wplt_wpl_properties.price,
            wplt_wpl_properties.price_unit,
            wplt_wpl_properties.lot_area,
            wplt_wpl_properties.lot_area_unit,
            wplt_wpl_properties.build_year, 
            wplt_wpl_property_types.id,
            wplt_wpl_property_types.name,
            wplt_wpl_units.id AS units_id,
            wplt_wpl_units.name AS units_name
            FROM wplt_wpl_properties 
            INNER JOIN wplt_wpl_property_types 
            ON wplt_wpl_properties.property_type=wplt_wpl_property_types.id INNER JOIN wplt_wpl_units ON units_id=wplt_wpl_properties.price_unit";

HTML / PHP部分

 <tr>
                    <td style="vertical-align: middle;"><?php echo $count; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['mls_id']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['name']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['location1_name']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['price']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['units_name']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['lot_area']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['lot_area_unit']; ?></td>
                    <td style="vertical-align: middle;"><?php echo $row['build_year']; ?></td>
                </tr>

但它没有显示任何结果。

2 个答案:

答案 0 :(得分:0)

你有

wplt_wpl_units.id AS units_id 

在您的选择和

units_id=wplt_wpl_properties.price_unit

在你的ON子句中,最后一位应该是

id=wplt_wpl_properties.price_unit

答案 1 :(得分:0)

我弄错了,没看见。应该是tmp而不是ON units_id=wplt_wpl_properties.price_unit INNER JOIN wplt_wpl_units ON wplt_wpl_units.id=wplt_wpl_properties.price_unit