它适用于过滤器表单。我有两张这样的桌子:
巡航(身份证,姓名,[...])
cruise_itineraries(id,cruise_id,port_name,departure_time,[...])
如果我选择通过某个港口的所有游轮,我会做这样的事情:
SELECT cruise.*
FROM cruise,
cruise_itineraries
WHERE cruise_itineraries.cruise_id = cruise.id AND cruise_itineraries.port_name LIKE %?%
但是,我希望在选择的端口中获得所有巡航开始它的路线,这意味着这必须是来自cruise_itineraries查询的第一条匹配cruise_id的记录。 我尝试过这样的事情:
SELECT cruise.*
FROM cruise,
cruise_itineraries
WHERE (
SELECT port_name
FROM cruise_itineraries,
cruises
WHERE cruise_itineraries.cruise_id = cruise.id
ORDER BY cruise_itineraries.id ASC LIMIT 1)
LIKE %?%
这意味着,获取每个ID的第一个记录并将其与%?%匹配,但是我没有得到任何结果。
我该怎么做呢?
答案 0 :(得分:0)
看起来你只想要港口最早离开的行程。
SELECT cruise.*
FROM cruise
INNER JOIN cruise_itineraries ci ON
ci.cruise_id = cruise.id
AND ci.port_name LIKE %?%
AND ci.departure_time = (select min(ci2.departure_time)
from cruise_itineraries ci2
where ci2.cruise_id = ci.cruise_id and ci2.port_name= ci.port_name
group by ci2.cruise_id, ci2.port_name)