Query to return data in existations

Dúvidas, dicas e truques de SQL, Select, Update, Delete, cláusulas, operações com joins, Funções em SQLs, etc
Post Reply
Moloukos
Rank: Programador Sênior
Rank: Programador Sênior
Posts: 54
Joined: Thu, 18 Sep 2008 10:22 pm
Location: São Paulo - SP
Moloukos Rox

Good afternoon.

I would like your help in Query below:

Select all

 SELECT C.* 
        FROM TB_CENTRAL C 
       WHERE EXISTS (SELECT DISTINCT 1 
                                FROM TB_INTERMEDIARIA I 
                              WHERE I.ID_ETA IN (16,24) 
                                   AND I.ID = C.ID) 
Is there any form or function that only returns the data case The two records of IN (16 and 24) exist in the table tb_intermediary. Because in this case if I inform only the 16 data are already returned.
Example:

Select all

 SELECT C.* 
        FROM TB_CENTRAL C 
       WHERE EXISTS (SELECT DISTINCT 1 
                                FROM TB_INTERMEDIARIA I 
                              WHERE I.ID_ETA IN (16) 
                                   AND I.ID = C.ID) 
What precise is only to return data if the two records actually exist in the table ....
User avatar
tiago_pimenta
Rank: DBA Júnior
Rank: DBA Júnior
Posts: 213
Joined: Wed, 29 Jun 2011 9:49 am
Location: Barretos / SP

Do you necessarily need to use the IN?
User avatar
stcoutinho
Moderador
Moderador
Posts: 850
Joined: Wed, 11 May 2011 5:15 pm
Location: são Paulo - SP

Moloukos,

You could make a desire similar to this:

Select all

SELECT C.* 
  FROM TB_CENTRAL C 
 WHERE EXISTS (SELECT 1 
                 FROM TB_INTERMEDIARIA I 
                WHERE I.ID_ETA =  16 
                  AND I.ID = C.ID) 
   AND EXISTS (SELECT 1 
                 FROM TB_INTERMEDIARIA I 
                WHERE I.ID_ETA =  24 
                  AND I.ID = C.ID)

Hugs,
[ 99] sergio
Moloukos
Rank: Programador Sênior
Rank: Programador Sênior
Posts: 54
Joined: Thu, 18 Sep 2008 10:22 pm
Location: São Paulo - SP
Moloukos Rox

Tiago,

I do not necessarily need to use the IN, but I need to know if the past values ??are exactly equal to those registered in the bank.

I receive a string with values ??12, 5, 33 --- These values ??can be changed in each query.
In the next I can receive 5, 33

and I have to go to the table and validate if it is exactly equal to the registered.
The rows in the table are so for registration 1:

Select all

TB_INTERMEDIARIA id = 1 , ID_ETA = 5 
TB_INTERMEDIARIA id = 1 , ID_ETA = 33
Moloukos
Rank: Programador Sênior
Rank: Programador Sênior
Posts: 54
Joined: Thu, 18 Sep 2008 10:22 pm
Location: São Paulo - SP
Moloukos Rox

Sergio,

That way I have a problem because the string I get is dynamic.

I can receive at the first call 12, 5, 33
in the second call 5, 33

there would have to dynamically mount the amount of existations The way you passed to assemble Query.
User avatar
dr_gori
Moderador
Moderador
Posts: 5024
Joined: Mon, 03 May 2004 3:08 pm
Location: Portland, OR USA
Contact:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

Yeah, I think if your string is dynamic, the best thing to do is a dynamic WHERE clause. But preferably, using binds in dynamic SQL.
User avatar
stcoutinho
Moderador
Moderador
Posts: 850
Joined: Wed, 11 May 2011 5:15 pm
Location: são Paulo - SP

Moloukos,

Remembering that within an "in" you can only "nest" up to 1000 elements. If they are more than 1,000 you will have to put the lists in multiple commands, each of them containing up to 1,000 elements.

Hugs,

Sergio
juliocpgr
Rank: Estagiário Júnior
Rank: Estagiário Júnior
Posts: 1
Joined: Thu, 13 Aug 2015 5:23 pm

Good morning friends.

expensive moloukos,

If you are still needing help (despite the time), the answer is yes.

Your situation can be solved easily using the "= ALL" clause.
Example:

Select all

 select *  
     from tabela  
   where campo   =ALL  (1,2,3,4) 
In this case, the query will only result if all values ??of the list (1,2, 3.4) are in the field.

In addition to ALL, there are other comparison conditions such as Any and Add.

Embrace.
Júlio César
Moloukos
Rank: Programador Sênior
Rank: Programador Sênior
Posts: 54
Joined: Thu, 18 Sep 2008 10:22 pm
Location: São Paulo - SP
Moloukos Rox

Júlio, thanks for the return.

I will use in the next implementations.
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 4 guests