子查询并从两个表中获取数据

Sub Queries and fetching data from two tables

我需要为每个 horse 列出 horse_idhorse_name 排在前 3 位 (例如,位置 1 ,2 或 3) 两次或更多次 次。提示:尝试使用 WHERE...IN...

我很确定我必须从 HORSE table 获取数据,其中包括 horse_idhorse_name,以及来自 PRIZE table,其中包括列 PLACE(放置马)。但是 PLACE table 只有 EVENT_id, PlaceMoney 列,所以我不知道如何加入马 [=31] =].

很难辨认出你的表格和数据,因为你是在评论而不是问题中发布的,但试试这个。

select horse.* 
  from horse
    inner join entry
      on horse.horse_id = entry.horse_id
  where entry.place <= 3
  group by horse.horse_id
  having count(horse.horse_id) >= 2
SELECT horse_id,name
FROM HORSE
WHERE horse_id IN
(SELECT horse_id
FROM ENTRY
WHERE place <= 3
GROUP BY horse_id
HAVING count(horse_id) > 1);