如果一个表没有记录,MySQL加入三个表

时间:2016-05-29 06:39:40

标签: mysql select join

$numQuery1 = "
SELECT d.*
     , r.*
     , AVG(c.on_time)+AVG(c.friendly)+AVG(c.language_skills)+AVG(c.professional) ranking
  FROM comment c
  LEFT 
  JOIN driver d
    ON d.userid = c.driver_id
  LEFT 
  JOIN driver_rental r
    ON r.email = d.email 
 WHERE 
     ( 
       $driver_rental IS NOT NULL 
   AND $driver_rental != ''
   AND car_type_1 >= $_SESSION[car_type_1] 
     )
    OR car_type_2>=$_SESSION[car_type_1]);
";

如果注释表没有driver_id记录,结果为null,我可以忽略注释表但是在select语句中不能没有吗?

1 个答案:

答案 0 :(得分:0)

$numQuery1 = "SELECT driver.*, driver_rental.*, avg(comment.on_time)+avg(comment.friendly)+avg(comment.language_skills)+avg(comment.professional) as ranking
    FROM comment
    LEFT JOIN driver
    ON
    driver.userid = comment.driver_id
    RIGHT JOIN driver_rental
    ON
    driver.email = driver_rental.email
    WHERE
    ($driver_rental IS NOT NULL AND $driver_rental != '') AND (car_type_1>=$_SESSION[car_type_1] OR car_type_2>=$_SESSION[car_type_1])";

这个想法是不要强调任何一个连接中的驱动程序表。因此,而不是第二个左连接使用右连接与driver_rental