3

Let's say I have 2 database tables:

table B is a set of people, table A is a set of people from table B

Table A = (no, id, date) no is PK, id refer to table B

Table B = (id, name) id is PK

My goal is to get data of people (id and name) who didn't attend on a given date (like today for example) the theory seems simple, set of B substract by set of A who attend (in today) but how can I do this in SQL query? I think about first query substracted by second query but got confused.

2 Answers 2

3
select * from b where 
not exists (select no from A where A.id=B.id and date=@yourdate)
2
  • @yourdate isn't postgresql style ($1 would be for example)
    – araqnid
    Aug 17, 2012 at 10:40
  • 1
    wow, what a quick reply. thanks a lot. it's exactly the result what i want Aug 17, 2012 at 10:41
1

If I understood properly it would be something like this:

select b.id, b.name from tableB b where b.id not in (
   select b.id from tableA a
   inner join tableB b on a.id = p.id
   where a.date = CURRENT_DATE)
7
  • 3
    convert is not a PostgreSQL function (not even a standard SQL function)
    – user330315
    Aug 17, 2012 at 10:30
  • 2
    There is no need to convert a DATE to a string literal. Just compare the two dates directly. (And getdate() is not a PostgreSQL function either)
    – user330315
    Aug 17, 2012 at 10:40
  • @a_horse_with_no_name replaced with now(), but if I don't convert it to char, won't it compare the time as well?
    – aF.
    Aug 17, 2012 at 10:46
  • A PostgreSQL date column does not have a time part.
    – user330315
    Aug 17, 2012 at 11:23
  • @a_horse_with_no_name then to use the now() (that has the time part), I've to compare it like this right?
    – aF.
    Aug 17, 2012 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.