A
A
andreys752020-01-23 12:28:12
SQL
andreys75, 2020-01-23 12:28:12

How to write an optimal SQL query with a calculated field?

Good afternoon, please help me write an optimal SQL query
, there are three tables:

  1. Events
  2. Members
  3. Users

Accordingly, the user registering for the event becomes a participant. The Participants table implements a many-to-many relationship between Events and Users.
Nozhno, for a given user ID to make a selection of all events and add a calculated field of type boolen - whether this user is a participant in the event.
I want to do it in one request, and as quickly as possible

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2020-01-23
@andreys75

Questions of query optimization do not make sense without specifying the DBMS used.
or

select ..., exists(select null from participations as p on p.user_id = ? and e.id = p.event_id) from events as e

K
Konstantin Tsvetkov, 2020-01-23
@tsklab

Nozhno
No. It is enough to define there is a "User ID" in "Participants", a subquery, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question