If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > identifying common elements of two columns of two tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-11, 05:48
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
identifying common elements of two columns of two tables

I am trying to get the list of ids that appears in two tables:
select id1 from table1 where id1 IN (SELECT id2 from table2).
However this query does not give the desired results.
Why?
Reply With Quote
  #2 (permalink)  
Old 09-05-11, 05:56
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Can id2 be NULL? If yes, use the following

Code:
select id1 
from table1 
where id1 IN (SELECT id2 
              from table2
              WHERE id2 <> NULL)
Reply With Quote
  #3 (permalink)  
Old 09-05-11, 06:58
krontrex krontrex is offline
Registered User
 
Join Date: Sep 2010
Posts: 37
Is there any faster way to solve this? I mean to check if the elements of some columns are also in a column of some other table?
Thanks
Reply With Quote
  #4 (permalink)  
Old 09-05-11, 07:29
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
What exactly do you mean with "elements of some columns"?

Please show us the table definitions (as CREATE TABLE statements), some sample data (as INSERT INTO statements) and the output you expect to see.

And please use [code] tags to make your SQL readable.
For details on how to use the tag, please see: http://www.dbforums.com/misc.php?do=bbcode
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On