Discuss SELECT Statement in the PostgreSQL Help forum on Dev Shed. SELECT Statement PostgreSQL Help forum discussing administration, SQL syntax, or other PostgreSQL-related topics. PostgreSQL provides enterprise level database functionality at open source prices.
Posts: 6
Time spent in forums: 1 h 4 m 23 sec
Reputation Power: 0
SELECT Statement
Hello all. This is probably an easy one for you but I am having difficulty with it. Here goes.
Let's say I have two tables.
Table1 has two columns: id & name
Table2 has two columns: id & amount
Column id is customer identity in both tables. Customer in Table1 may or may not have records in Table2.
I want to select all customers from Table1 (whether or not they have records in Table2) and amount (Where amount > '2.00') from Table2.
This will help me greatly in supplying reports for our customers.
Posts: 6
Time spent in forums: 1 h 4 m 23 sec
Reputation Power: 0
Quote:
Originally Posted by MrFujin
Look at LEFT JOIN, something like this: (not tested)
Code:
SELECT *
FROM table1
LEFT JOIN tabel2
WHERE table2.amount > 2
Hello, thanks for the reply.
That WHERE clause will omit any records from table1 that do not meet that condition. I am looking to show all records from table1 whether or not they have any records in table2 but any records in table2 must meet the condition.
Posts: 2,685
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 53 m 13 sec
Reputation Power: 284
You need to move the condition for the amount into the JOIN clause (out off the WHERE clause)
Code:
select t1.id,
t1.name,
t2.amount
from table1 t1
left join table2 t2
on t1.id = t2.t1_id
and table2.amount > 2
__________________
I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
http://forums.devshed.com/misc.php?do=bbcode#code
Tips on how to ask better questions:
http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
http://wiki.postgresql.org/wiki/SlowQueryQuestions
http://catb.org/esr/faqs/smart-questions.html