INNER JOIN来自同一张桌子

时间:2015-12-07 16:07:27

标签: sql select inner-join

我需要在mysql同一个表“ride”中进行两个查询的内连接:

id| appointment_address|arrival_city| arrival_country|departure_city|departure_country|end_date|nb_places|price|start_date|travel_id|rank 

第一个查询:

SELECT DISTINCT travel_id FROM ride  WHERE departure_city LIKE <value> AND start_date > <value> OR departure_country LIKE <value> AND start_date > <value>;

第二个

SELECT DISTINCT travel_id FROM ride  WHERE arrival_city LIKE <value2> AND start_date > <value> OR departure_country LIKE <value2> AND start_date > <value>;

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你可以这样做。

select * 
from (SELECT DISTINCT travel_id FROM ride  WHERE departure_city LIKE <value> AND start_date > <value> OR departure_country LIKE <value> AND start_date > <value>) firstQuery

Join 
(SELECT DISTINCT travel_id FROM ride  WHERE arrival_city LIKE <value2> AND start_date > <value> OR departure_country LIKE <value2> AND start_date > <value>) secondQuery on firstQuery.<field_to_join > = secondQuery.<field_to_join >