2016-09-07 120 views
2

我想獲得一個查詢使用PHP。基本上它應該首先檢查沒有做出具體的預訂,然後查詢所有可用車輛和司機的數據庫。Mysql選擇IF函數子查詢

這是查詢至今:

IF EXISTS(SELECT * FROM bookings LEFT JOIN status WHERE bookings.status_id = '.$booking.') 
      BEGIN 
       select * 
       FROM bookings  as b 
       LEFT JOIN drivers as d where b.driver_id !="d.driver_id" 
       LEFT JOIN vehicles as v where b.vehicle_id !="v.vehicle_id" 
       LEFT JOIN status as s where b.status_id !="'.$booking.'" 
       ORDER BY b.bookingDate DESC 
      END 
+0

那麼,是什麼問題? –

+2

最簡單(也是正常)的方法是進行2個獨立的數據庫調用 –

+0

我需要IF()函數來測試是否有一些預訂,否則運行子查詢......此刻我只是得到一個錯誤,我嘗試了其他方法,但沒有運氣。我需要它來測試從php var $ booking提供的值。 –

回答

0

加入預訂:

SELECT * 
FROM bookings  as b 
LEFT JOIN drivers as d where b.driver_id !="d.driver_id" 
LEFT JOIN vehicles as v where b.vehicle_id !="v.vehicle_id" 
LEFT JOIN status as s where b.status_id !="'.$booking.'" 
CROSS JOIN (
    SELECT 1 
    FROM bookings 
    WHERE bookings.status_id = '.$booking.' 
    LIMIT 1 
) t 
ORDER BY b.bookingDate DESC