Filter a select, without duplicate SQL queries being returned

4

The problem is to return a SELECT with only the schedules that have not already been accepted by the user. I am trying to use the table of schedules that have already been accepted, which have both the user ID and the scheduling it has accepted to filter the schedules.

Accepted schedules table

| SCHEDULE_ID |   USER_ID    |
------------------------------
      25      |      26      |
      41      |      26      |
      41      |      26      |
      42      |      29      |

The keys come from the connection of the table with the following tables:

Schedule of schedules created

SCHEDULE_ID |    TITULO    |  DAILY_PRICE  |  CLIENT_ID  |
----------------------------------------------------------
 41         |  Padaria     |    290.00     |      1      |
 42         |  Confeitaria |    350.00     |      2      |
 25         |  Lavanderia  |    350.00     |      3      |

Registered delivery table

 USER_ID    |    NAME    |
--------------------------
 25         |   João     |
 26         |   Maria    |
 29         |   Claúdio  |

I do not have much custom with the use of SQL statements yet, so I could not develop a query that would return the values without repeating them for example.

    
asked by anonymous 29.09.2017 / 06:01

3 answers

1

To get to the desired result you should use the EXISTS clause together with NOT :

SELECT a.*
  FROM agendamentos a
 WHERE NOT EXISTS(SELECT 1
                    FROM agendamento_aceitos aa
                   WHERE aa.schedule_id = a.schedule_id
                     AND aa.user_id = 26)

In the above example will be shown schedules that have not yet been accepted by the deliverer 26 - Maria.

  

Subqueries with EXISTS or NOT EXISTS

     

If subquery returns any rows at all, EXISTS subquery is TRUE, and NOT EXISTS subquery is FALSE

Or in free translation:

  

If the subquery returns any line, EXISTS will be TRUE, and NOT EXISTS will be FALSE

    
29.09.2017 / 13:47
1

You must perform a join, join joins the results of two tables into one query, it uses a field to perform this join.

Example of desired join

Select Entregadores.NAME,
       Agendamento.TITULO,
       Agendamento.DAILY_PRICE,
       Agendamento.CLIENT_ID 
  from Tabela_de_agendamentos_que_foram_aceitos AgendamentoAceito
   join Tabela_de_agendamentos_criados Agendamento
     on (AgendamentoAceito.SCHEDULE_ID = Agendamento.SCHEDULE_ID)
   join Tabela_de_entregadores_que_aceitaram_os_agendamentos Entregadores
     on (AgendamentoAceito.USER_ID = Entregadores.USER_ID)

With all the join well positioned the records will not be repeated, unless they are repeated in the tables.

Talking a little more to join

Many developers have the difficulty of knowing what result is returned by each join in SQL, and therefore when to use each. To facilitate this understanding, Figure 1 presents a graphical representation, based on the Theory of Sets, well known in mathematics. In this image, we have the representation of two tables (A and B) and the expected result for each type of join (the area in red represents the records returned by the query).

Source: link

    
29.09.2017 / 06:29
-2

You can use distinct in the query to return unique values.

    
29.09.2017 / 10:51