如何将两个表格排在一起

时间:2013-12-03 13:27:04

标签: php mysql sql

如何一起显示两个表行 而我的代码是错误的,它显示空白结果可能是代码中的一些错误请帮我解决这个问题

感谢

我有两张桌子

1)公寓

------------------------------------------
flatsno | buildingname| flattype | status |
------------------------------------------
001    | building1   | Double   |  empty |
002    | building1   | single   |  empty |
003    | building1   | Double   |  empty |
004    | building1   | Double   |  empty |
005    | building2   | Double   |  empty |

2)合同


flatno | buildingname| flattype | status |
------------------------------------------
001    | building1   | Double   |  rent  |
002    | building1   | single   |  rent  |
005    | building2   | Double   |  rent  |

我想要那样的结果

结果

------------------------------------------
flatno | buildingname| flattype | status |
------------------------------------------
001    | building1   | Double   |  rent  |
002    | building1   | single   |  rent  |
003    | building1   | Double   |  empty |
004    | building1   | Double   |  empty |
005    | building2   | Double   |  rent  |

我正在使用此代码,但它显示空白结果

<?php


    $dbserver = 'localhost'; 
    $dblogin  = 'root';
    $dbpassword = '';  
    $dbname = 'building';

    //opening connection
    $mysqli = new mysqli($dbserver, $dblogin, $dbpassword, $dbname);
    if (mysqli_connect_errno()) 
    {
        printf("Connection failed: %s\n", mysqli_connect_error());
        exit();
    }


function formatMoney($number, $fractional=false) {
    if ($fractional) {
        $number = sprintf('%.2f', $number);
    }
    while (true) {
        $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
        if ($replaced != $number) {
            $number = $replaced;
        } else {
            break;
        }
    }
    return $number;
}       
$a=$_POST['from'];


    echo "<div id='non-printable'><table align='center' class='sortable' border='1' cellpadding='10'>";
    echo "<tr><th>Flatno</th><th>Buildingname</th><th>floor no</th><th>flatclass</th><th>flattype</th></tr>";

    //opening connection
    $result = $mysqli->query("SELECT `flatsno`, `buildingname`, `status`, `flattype` FROM `flats`  WHERE `flatsno` AND `buildingname` = '$a' ORDER BY `flatsno` ASC") or die($mysqli->error.__LINE__);
    while($student = $result->fetch_assoc())
    { 
       $subresult = $mysqli->query("SELECT * FROM `contract` WHERE `flatno` = '".$contract['flatno']."' AND `buildingname` = '$a'") or die($mysqli->error.__LINE__);
        if($row = $subresult->fetch_assoc())

        {
            echo "<tr>"; 
            echo '<td>' . $row['flatsno'] . '</td>';
            echo '<td>' . $row['buildingname'] . '</td>';
            echo '<td>' . $row['flattype'] . '</td>';
            echo '<td>' . $row['status'] . '</td>';
            echo "</tr>"; 

        }
        else
        {
            echo "<tr>";
        echo '<td>' . $contract['flatno'] . '</td>';
        echo '<td>' . $contract['buildingname'] . '</td>';
        echo '<td>' . $contract['flattype'] . '</td>';
        echo '<td>' . $row['status'] . '</td>';
        echo '<td></td>';


            echo "</tr>";
        }
    }
            echo '</table>';

mysqli_close($mysqli); 
?> 

3 个答案:

答案 0 :(得分:0)

我相信只有下面的SELECT和循环迭代结果集才能解决问题。

select f.flatsno, f.buildingname, c.flattype , c.status 
from flats f
     inner join contract c on (f.flatsno = c.flatsno )
where   <<  tradudor and desired order >>

我希望它有用

答案 1 :(得分:0)

我认为您只需使用1个查询即可获得所需的结果(而不是您当前使用的2个查询)。尝试使用此查询:

SELECT f.flatsno, f.buildingname, IFNULL(c.status,f.status) as status, f.flattype 
FROM `flats` f LEFT JOIN `contract` c
ON f.flatsno = c.flatno AND f.buildingname = c.buildingname
WHERE f.buildingname = "building1"
ORDER BY f.flatsno ASC

答案 2 :(得分:0)

我认为您尝试做的是将两个表的数据合并到一个新表中,对吧? 如果没有,你应该这样做,因为你的数据没有标准化。在大多数情况下,您希望数据库至少具有第三范式。如果您不知道我在说什么,那么我强烈建议您阅读这个主题。

无论如何,你可以像上面这样提到:

create table new_table like flats;

insert into new_table
select
f.flatsno, f.buildingname, f.flattype, coalesce(c.status, f.status)
from
flats f
left join contract c on f.flatsno = c.flatno and f.buildingname = c.buildingname;

select * from new_table;