2014-04-21 81 views
0

請問如何將這個查詢與內部連接轉換?與內部連接相交

(SELECT DISTINCT tables.table_id AS 'Available table ID' 
    FROM tables, reservation 
    WHERE tables.table_id = reservation.table_id 
    AND ((start_time <> ? AND date = ?) 
      OR (date <> ?)) 
      OR status = 1) 
INTERSECT 
(SELECT DISTINCT tables.table_id AS 'Available table ID' 
    FROM tables, reservation 
    WHERE tables.table_id = reservation.table_id 
    AND ((start_time <> ? AND date = ?) 
      OR (date <> ? AND reservation.table_id <> ?)) 
      OR status=1) 
+1

此無關的Java。我編輯了這個問題來刪除'Java'標籤,但只是單挑。 – yshavit

+0

希望查詢現在可讀性更高 – JonK

+0

@Strawberry問題標籤爲MySQL,它沒有'INTERSECT'關鍵字。 – Air

回答

-1

你可以試試這個:(我不知道,如果你正在尋找這個答案)

(SELECT DISTINCT tables.table_id AS 'Available table ID' 
    FROM tables JOIN reservation 
    ON (tables.table_id = reservation.table_id) 
    WHERE start_time <> ? AND date = ? 
     OR (date <> ?)) 
     OR status = 1 
INTERSECT 
(SELECT DISTINCT tables.table_id AS 'Available table ID' 
    FROM tables JOIN reservation 
    ON (tables.table_id = reservation.table_id) 
    WHERE start_time <> ? AND date = ? 
     OR date <> ? AND reservation.table_id <> ?) 
     OR status=1