A
A
AlexSer2020-04-24 10:25:41
SQL
AlexSer, 2020-04-24 10:25:41

How to select records that have 2 or more related records in another table?

How to select records that have 2 or more related records in another table?
for such a scheme:
5ea294946f6fc799473511.png
That is, if there are 2 directions or more by id_history, then choose.
Step 2 - then from these two selected, choose the one with the smallest id;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pankov, 2020-04-24
@AlexSer

I'm scared to go to the clinic now.
Would you at least suggest some table names for clarity, so as not to compose A and B.

SELECT rr.* 
FROM (
  SELECT MIN(r.id) AS id
  FROM recept r
  JOIN history h ON r.id_history = h.id
  GROUP BY r.id_history
  HAVING COUNT(r.id) >= 2
) ids
JOIN recept rr ON rr.id = ids.id

D
d-stream, 2020-04-24
@d-stream

In principle, it is enough to translate the text of the description into English, remove the parasitic words and get an sql query

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question