Friday, October 23, 2009

How to put a SELECT expression within a SELECT expression in postgreSQL

Have you ever been into a situation where you wanted to get the datas from a certain table where your condition will come from another table in postgreSQL? One of the methods to do that is to create a select statement inside a select statement. Here is a little demonstration of the code.

Suppose you had a table named table1 and you have the fields
id
first_name
middle_name
last_name
in there.

and in another table named table 2 you have the fields
id
table1_id
full_name
address

let us suppose that you wanted to get all the first_name from table1 where the table1 id is not equal to the id from the table2. I guess there are several ways to do this but the one that I will show to you is the NOT IN expression. You can also use the IN expression which is the opposite of the NOT IN.

Here is the code....

SELECT first_name FROM table1 WHERE id NOT IN (SELECT id FROM table2);

This may be a simple code but it can help you when the time comes.....



No comments:

Post a Comment

Feeds

Sonic Run: Internet Search Engine javascript, coldfusion, programming, simple programming teckniques, css, html Webfeed (RSS/ATOM/RDF) submitted to http://www.feeds4all.nl Web Development & Design Blogs DigNow.net

Search Engine Optimization and SEO Tools
javascript, coldfusion, programming, simple programming teckniques, css, html