Here is a DB example
table "Users"
fname | lname | id | email 
Joe   | smith | 1  | yadda@goo.com
Bob   | smith | 2  | bob@goo.com
Jane  | smith | 3  | jane@goo.com
table "Awards"
userId | award 
1      | bigaward
1      | smallaward
1      | thisaward
2      | thataward
table "Invites"
userId | invited
1      | true
3      | true
Basically, how do you write a query in PostgreSQL that allows you to create something like this:
[{
     fname:"Joe",
     lname:"Smith",
     id: 1,
     email: "yadda@goo.com",
     invited: true,
     awards: ["bigaward", "smallaward", "thisaward"]
 },
 {
     fname:"Jane",
     lname:"Smith",
     id: 3,
     email: "jane@goo.com",
     invited: true,
     awards: []
 }]
Here is what I am trying to do...
SELECT users.fname, users.lname, users.id, users.email, invites.invited, awards.award(needs to be an array)
FROM users
JOIN awards on ....(unknown)
JOIN invites on invites.userid = users.id
WHERE invited = true
the above array would be the desired output, just can't figure out a good one shot query. I tried the PostgreSQL docs, but to no avail. I think I might need a WITH statement?
Thanks in advance, Postgres guru!
PostgreSQL v. 9.2
Answered by RhodiumToad on the postgresql IRC:
SELECT users.fname, users.lname, .... array(select awards.award from awards where a.id = user.id) as awards
FROM users
JOIN invites on invites.userid = users.id
WHERE invited = true
array() then through a query inside of it... brilliant!
 
     
    