繁体   English   中英

如何将两个表排在一起

[英]how to get two table row together

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

谢谢

我有两个桌子

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

我相信只有下面的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个查询(而不是当前使用的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

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

无论如何,您可以像这样实现上述目的:

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;

暂无
暂无

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

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