Write a request that will select the name of the event (events.caption), for which more than three applications.

There are two tables

first events table

  • id captoin
  • 1 first
  • 2 second
  • 3 thirdth

    second table bids

  • id id_event name

  • 1 1 aa

  • 2 2 bb
  • 3 2 cc
  • 4 2 dd
  • 5 2 ee
  • 6 1 ff
SELECT id_event, COUNT(id_event) AS cnt FROM bids GROUP BY id_event HAVING cnt NOT BETWEEN 0 AND 3; 

Here is the query to display id_event (2) and the number of cnt4

  • id_event cnt
  • 2 4

That is, it remains only to compare the variables id (from the events table) and id_event (from the bids table) so that the output will be the name of the event.

  • What field are you linking the table to? - Vitaly Shebanits pm
  • id and id_event, that is, if id_event = 1 then id = 1 displays the name of the first event - Vlad Boyko
  • Group, count orders, sort by decrease, take the first - Akina
  • I wrote a query that prints id_event and the number of cnt SELECT id_event, COUNT (id_event) AS cnt FROM bids GROUP BY id_event HAVING cnt NOT BETWEEN 0 AND 3; but it remains only to compare the id (events table) c (from the bids table) so that the output is the name of the event. - Vlad Boyko

1 answer 1

Use join to get the name of the event.

 SELECT caption, COUNT(b.id_event) AS cnt FROM bids b join events e on b.id_event=e.id GROUP BY e.caption HAVING cnt>3; 
  • Thank you very much! Everything is working. - Vlad Boyko