SQL JOIN但包含空值

时间:2014-12-10 12:38:30

标签: mysql

如果我在一个表中有15行并且我将另一个表连接到它,即使我想要加入值为null的值,我仍然可以输出全部15个吗?基本上你可以加入+包括连接结果中的空值。例如:

1
2
3
4
5
6

加入后只有3个具有连接参数

2
4
5

我如何展示一切?

我尝试了以下内容:

"SELECT * 
        FROM `ultrait_wpl_properties` 
        JOIN ultrait_wpl_property_types 
        ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
        ORDER BY ultrait_wpl_properties.id"

此部分输出重复的ID?

 $sql = "SELECT * 
        FROM `ultrait_wpl_properties` 
        LEFT JOIN ultrait_wpl_property_types 
        ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
        ORDER BY ultrait_wpl_properties.id ASC";
$result = mysqli_query($con, $sql); // Connect and run query

$dom = new DOMDocument(); // New DOM

$root = $dom->createElement('root'); // Create parent or root node
$dom->appendChild($root); // Append the root tag to the DOM

while($r = mysqli_fetch_assoc($result)){

    $node = $textContent = null; // $node will equal $textContent which is null
    $property = $dom->createElement('property'); // Create containing node

    foreach($r as $column_name => $val) { // Loop through key value pairs
        // so loop all the values on each row

2 个答案:

答案 0 :(得分:0)

试试这些:

"SELECT * 
    FROM `ultrait_wpl_properties` 
    LEFT JOIN ultrait_wpl_property_types 
    ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
    ORDER BY ultrait_wpl_properties.id"

OR:

"SELECT * 
    FROM `ultrait_wpl_properties` 
    RIGHT JOIN ultrait_wpl_property_types 
    ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
    ORDER BY ultrait_wpl_properties.id"

shree.pat18是正确的,要么取决于你的情况要使用INNER,RIGHT OR LEFT JOIN

答案 1 :(得分:0)

为什么不试试left join

SELECT * 
        FROM `ultrait_wpl_properties` 
        LEFT JOIN ultrait_wpl_property_types 
        ON ultrait_wpl_properties.property_type = ultrait_wpl_property_types.id
        ORDER BY ultrait_wpl_properties.id