简体   繁体   English

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

[英]MySQL Join three table if one table no record

$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]);
";

if the comment table have no driver_id record, the result is null, can I ignore comment table but in select statement cannot without it?如果注释表没有 driver_id 记录,结果为空,我可以忽略注释表但在 select 语句中不能没有它吗?

$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])";

The idea is to not stress on driver table in either join.这个想法是不要在任何一个连接中强调驱动程序表。 SO instead of second left join use right join with driver_rental SO 而不是第二个左连接使用带有 driver_rental 的右连接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM