[英]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.