php的子查询

时间:2017-11-19 21:47:38

标签: php mysql

抱歉,如果这可能是一个重复的问题,但我非常绝望地需要项目提交方面的帮助。

我有一个数据库停车场,我有2个表,其中有carpark_availability和历史。

从我的代码中,我能够得到一个领域"开发"来自table carpark_availability。 如果我想从c中选择我的h.development = c.development,我想询问如何构造我的查询。

如下所示是我的代码:

<?php
session_start();
    $con = new mysqli('localhost','root','','carpark_project');
    $dev = $_SESSION["development"]; 
    echo "Development: ";
    echo $dev;



    $sql = "select * from carpark_availability where 
            carpark_availability.Development IN (select history.development 
            from history where
            history.Development = '$dev' )";
   //$sql = "select * from carpark_availability where development = '$dev'";
   //$sql = "select * from carpark_availability where (select Development
   //from history where Development= '$dev'";

   $result = $con->query($sql);


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

        echo "Development: ";
        echo $row["Development"]. "</br>";
        echo $row["Area"]."</br>";
        echo $row["weekday1"]."</br>";
    } 

 ?>

1 个答案:

答案 0 :(得分:1)

连接比子查询更容易。

SELECT c.*
FROM carpark_availability c
INNER JOIN history
USING (development)
GROUP BY development;

我猜你得到了一个错误,因为你的查询没有返回任何结果,所以$result被设置为布尔false。您的代码中没有任何内容可以检查,因此您尝试在其上调用fetch_assoc()