mysql查询问题更多左连接

时间:2016-10-20 11:31:34

标签: php mysql

我的mysql查询如下:

SELECT 
    `pickupreq`.*, 
    `pinP`.`Pincode` as PinP, 
    `pinD`.`Pincode` as PinD, 
    `tslots`.`StartTime`, 
    `tslots`.`EndTime`, 
    `usr`.`FirstName`, 
    `usr`.`LastName`, 
    `staff`.`FirstName` as sFirstName, 
    `staff`.`LastName` as sLastName, 
    `ord`.`Id` as OrderId, 
    `ord`.`NoOfItems`, 
    `ord`.`NoOfItemsReadyForDelivery`, 
    `ord`.`CurrentStatus`, 
    `ord`.`TotalAmount`, 
    `ord`.`PickupSignature` 
FROM 
    (`PickupRequests` as pickupreq) 
    LEFT JOIN `Users` as usr ON `usr`.`Id` = `pickupreq`.`UserId` 
    LEFT JOIN `Users` as staff ON `staff`.`Id` = `pickupreq`.`AssignedTo` 
    LEFT JOIN `TimeSlot` as tslots ON `tslots`.`Id` = `pickupreq`.`TimeSlotId` 
    LEFT JOIN `Orders` as ord ON `ord`.`PickupRequestsId` = `pickupreq`.`Id` 
    LEFT JOIN `UserAddresses` as addrpick ON `addrpick`.`Id` = `pickupreq`.`PickupAddressId` 
    LEFT JOIN `Country` as countryP ON `countryP`.`Id` = `addrpick`.`CountryId` 
    LEFT JOIN `State` as stP ON `stP`.`Id` = `addrpick`.`StateId` 
    LEFT JOIN `City` as cityP ON `cityP`.`Id` = `addrpick`.`CityId` 
    LEFT JOIN `Pincode` as pinP ON `pinP`.`Id` = `addrpick`.`PincodeId` 
    LEFT JOIN `UserAddresses` as addrdeli ON `addrdeli`.`Id` = `pickupreq`.`DeliveryAddressId` 
    LEFT JOIN `Country` as countryD ON `countryD`.`Id` = `addrdeli`.`CountryId` 
    LEFT JOIN `State` as stD ON `stD`.`Id` = `addrdeli`.`StateId` 
    LEFT JOIN `City` as cityD ON `cityD`.`Id` = `addrdeli`.`CityId` 
    LEFT JOIN `Pincode` as pinD ON `pinD`.`Id` = `addrdeli`.`PincodeId` 
WHERE 
    `pickupreq`.`PickedUpStatus` = 'Yes' 
    AND `pickupreq`.`Status` <> 'CancelRequest' 
    AND `ord`.`CurrentStatus` NOT IN ('Completed', 'Delivered') 

上面的表中没有得到值为NULL的记录和Orders表的PickupRequestsId中的0。 我该怎么做才能从Orders表的PickupRequestsId列中获取 NULL 0 值记录

1 个答案:

答案 0 :(得分:1)

     LEFT JOIN `Orders` as ord ON `ord`.`PickupRequestsId` = `pickupreq`.`Id` 
+-->
|    LEFT JOIN `UserAddresses` as addrpick ON `addrpick`.`Id` = `pickupreq`.`PickupAddressId` 
|    LEFT JOIN `Country` as countryP ON `countryP`.`Id` = `addrpick`.`CountryId` 
|    LEFT JOIN `State` as stP ON `stP`.`Id` = `addrpick`.`StateId` 
|    LEFT JOIN `City` as cityP ON `cityP`.`Id` = `addrpick`.`CityId` 
|    LEFT JOIN `Pincode` as pinP ON `pinP`.`Id` = `addrpick`.`PincodeId` 
|    LEFT JOIN `UserAddresses` as addrdeli ON `addrdeli`.`Id` = `pickupreq`.`DeliveryAddressId` 
|    LEFT JOIN `Country` as countryD ON `countryD`.`Id` = `addrdeli`.`CountryId` 
|    LEFT JOIN `State` as stD ON `stD`.`Id` = `addrdeli`.`StateId` 
|    LEFT JOIN `City` as cityD ON `cityD`.`Id` = `addrdeli`.`CityId` 
|    LEFT JOIN `Pincode` as pinD ON `pinD`.`Id` = `addrdeli`.`PincodeId` 
|   WHERE 
|    `pickupreq`.`PickedUpStatus` = 'Yes' 
|    AND `pickupreq`.`Status` <> 'CancelRequest' 
+--- AND `ord`.`CurrentStatus` NOT IN ('Completed', 'Delivered')