0

Solved. Look in comment.

I am using PostgreSQL 9.1. I have 3 tables:

  • simcards: id serial NOT NULL, iccid character varying(30) NOT NULL, customer_id integer
  • customer: id serial NOT NULL, name character varying(50) NOT NULL
  • topup: id serial NOT NULL, amount integer NOT NULL, iccid_id integer NOT NULL

And now when I select some simcards I would like to get extra column that shows value yes/no, true/false, 1/0 if this simcard has also a record in topup table (if had been toped up).

I think I have to use LEFT JOIN but I don't know how to display only value true/false. Here is my current SQL that shows everything form topup table:

select
   b.iccid,
   b.phone_number,
   d.name as customer,
   t.*
from
   simcard_simcard as b
   join simcard_customer as d
       on b.customer_id = d.id   
   left join simcard_topup as t
    on b.id = t.iccid_id
where d.name = 'THREE'

Can you give me any suggestions? Thanks

  • replace t.* with t.id IS NOT NULL. That should return true if it's not null (it exists) and false otherwise. – Simo Kivistö Apr 16 '15 at 20:37
  • Thanks! Works great! I knew it would be something simple, but I couldn't figure this out. Thanks again. – gerpaick Apr 16 '15 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.