【问题标题】:Inner Join Query PHP issue MySQL内连接查询 PHP 问题 MySQL
【发布时间】:2014-04-18 18:18:11
【问题描述】:

我有两张桌子 1) live_booking 表 2) company_car 表

公司车PK是car_id。在预订表中有一个带有 car_id 的字段供用户输入。

问题在于,当用户运行 SQL 查询时,如果预订表中没有 car_id,则它不会返回任何结果,但是如果用户将 car_id 存在于预订表中,则 SQL 查询会显示所有结果.

我希望如果预订表中没有 car_id,则查询会从预订表中获取剩余字段并将汽车字段显示为空白。

$bookingid = $_GET['id']; 

$result = mysqli_query($con, "
SELECT 

live_booking.booking_id, live_booking.booking_time, live_booking.booking_date, live_booking.id_mobile,live_booking.reservation_time, live_booking.car_id, 

company_cars.car_driver, company_cars.car_make, company_cars.car_model,

customer.eu_name


FROM live_booking live_booking
inner join customer customer on customer.id_mobile = live_booking.id_mobile

inner join company_cars company_cars on company_cars.car_id = live_booking.car_id

WHERE booking_id = $bookingid");

【问题讨论】:

  • 去掉sql server标签,你的php建议mysql。
  • INNER JOIN 查找两个数据库之间共有的记录。你可能想要一个 LEFT JOIN。请参阅stackoverflow.com/questions/5706437/… 了解更多信息。 LEFT JOIN 将为您提供第一个表中的所有记录,然后加入额外表中的字段。
  • 为什么要用 car_id 和 id_mobile 链接表格?

标签: php mysql sql sql-server


【解决方案1】:

你可以在这里使用 mysqli_num_rows 和 if 语句 if No result then show the rest of all results

喜欢

     $row_cnt = $result->num_rows;
        if ($row_cnt == 0)

        {
        do this 
        }
       else 
        {
       do this
        }

有关更多信息,请查看此 http://www.php.net/manual/en/mysqli-result.num-rows.php

加上选择权标签:)

【讨论】:

    【解决方案2】:

    我只需要使用左连接而不是内连接。 http://www.w3schools.com/sql/sql_join_left.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多