I've got a table structure with these three tables in my PostgreSQL 9.2.8 database:
I'm trying to figure out how to select an orders
row and on the SAME row add some of the columns from order_points
and points
.
You can think of the points
table like a list of all items that a person could purchase, where it's known internally by the abbrev
and it costs amount
.
The order_points
table is each item purchased, so points.id == order_points.points_id
, and amount
would be similar to saying they bought 5 candy bars. It links to an orders via order_points.order_id == orders.id
When I select an order, I want to have a column for each abbrev
that exists as well as the amount
from the order_points
table.
So if points
has this:
id | name | abbrev | amount
--------------------------------
1 | Snickers | sn | 1.34
2 | Milky Way | mw | 1.73
3 | Coffee | cf | 10.12
and order_points
has this:
id | order_id | points_id | amount
----------------------------------
1 | 1 | 1 | 10
2 | 1 | 3 | 1
Then when I get my row I want all the columns from the order, plus three extra columns. I don't want to list every single column shown in orders above but basically assume I just wanted 4 of them, plus all the points
stuff I'd end up with this as a row of output:
id | created | due | name | sn | mw | cf
------------------------------------------------
1 | 2018-04-21 | 2018-05-01 | Fooey | 10 | 0 | 1
I've no idea how to dynamically add a column with a name (the abbrev
) from a table lookup.