简体   繁体   English

PHP:如何从另一个表中选择和显示?

[英]PHP: How to select and display from another table?

I have two tables in my database. 我的数据库中有两个表。 One labeled with tempahanbilik and the other one is bilik. 一个标有tempahanbilik,另一个是bilik。 Both tables have a column named id (which hold the same value). 两个表都有一个名为id的列(具有相同的值)。 Now, i want to display the other data that is not in tempahanbilik's table 现在,我想显示tempahanbilik表中没有的其他数据

Below is the code: 下面是代码:

$query = "SELECT * FROM tempahanbilik 
        WHERE tarikh BETWEEN '".$_SESSION['tarikh']."' AND '".$_SESSION['tarikh2']."'
    && tempahanbilik.id NOT IN (SELECT bilik.id FROM bilik)";


$results = $mysqli->query($query) or die($mysqli->error._LINE_);




$q = "SELECT * FROM bilik";



$result = $mysqli->query($query) or die($mysqli->error._LINE_);

   $total = $results->num_rows;
    if($total>0)
    {
        while($rows=$results->fetch_assoc())
        {   

            $nama = $rows['tempat'];
             echo" <tr>
         <td align = center><a href='tengokKosong2.php?tempat=$nama'>$nama</a></td>";

         echo "</tr>";
        }

    }
    else
    {
        while($row=$result->fetch_assoc())
        { 

            $id=$row['id'];
            $nama =$row['nama'];

         echo" <tr>
         <td align = center><a href='tengokKosong2.php?id=$id&nama=$nama'>$nama</a></td>";

         echo "</tr>";

        }
    }

No need to join tables, you can use sub query . 无需联接表,可以使用sub query your query should be like this 您的查询应该是这样的

$query = "SELECT * FROM tempahanbilik 
    WHERE ( tarikh BETWEEN '".$_SESSION['tarikh']."' AND '".$_SESSION['tarikh2']."'
) AND tempahanbilik.id NOT IN (SELECT bilik.id FROM bilik)";

You want to display the info from bilik that is not in tempahanbilik right? 您要显示来自bilik的不在tempahanbilik中的信息吗?

$query = "SELECT * FROM bilik 
WHERE id NOT IN (SELECT id FROM tempahanbilik WHERE 
tarikh BETWEEN '".$_SESSION['tarikh']."' AND '".$_SESSION['tarikh2']."')";

Please try using the above query and let me know it have result or not.. 请尝试使用上面的查询,并让我知道它是否有结果。

暂无
暂无

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

相关问题 PHP-SQL如何从表中选择ID并插入到另一个表中? - PHP - SQL how to select ids from table and insert into another table? 从一个表中选择所有值,从另一个表中选择一些值,并用php显示它们 - select all the values from one table and some values from another table and display them with php 使用PHP从MySQL表中选择并显示 - Select and display from MySQL table with php 我如何从表 (id) 中的 select 项目显示另一个表中匹配的 id? - How do i select items from a table(id) to display matched id from another table? PHP如何显示mysql选择查询表 - PHP How to display mysql select query to table 如何从表单元格值设置 select 选项并在另一个 html 表中显示数据库行值 rest - How to set select option from table cell value and display rest of database row value in another html table 为什么是mysql-从表中选择数据并显示到php,当将其插入mysql中的另一个表时,它仅保存1个值? - why mysql- Select data from table and display to php and when inserting it to another table in mysql it save only 1 value? 如何显示一个表中的数据,然后将该数据插入到php中的另一个表中? - How to display data from one table and then insert that data to another table in php? 如何在php中将一个表中的变量用作另一表的SELECT参数 - How can I use a variable from one table as a SELECT parameter for another table in php 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM