The query required for what I'd like to is beyond my SQL knowledge so I'm hoping to get some help here. I want to get releationships from multiple tables into one in postgreSQL 9.2. I know the procedure but I don't know the SQL.
There are 4 tables that will go into this query:
- wishlist: is associated with one or more lists and has prefered stores
- list_wishlist: holds the relationship between lists and wishlists
- item: is associated with a list and has prefered stores
- prefered_stores: this holds the relationsship between stores and prefered stores (Each prefered store is a separate row in prefered_store. Thus if an item or wishlist has more than one prefered store, prefered_store.id will be the same for those rows)
The tables look like this (with irrelevant columns removed):
And here is what the resulting table would look like:
Let me explain the result table:
- item_id: just that, id of item
- item_stores_comments: store/comment pairs from all prefered_stores rows associated with this item (store/comment separated by comma and pairs separated by semicolon)
- wishlist_stores: store ids of prefered_stored of wishlists that are associated with lists that this item is in (separated by commas)
I've filled the item_info table with the actual result for the example tables above so I guess it should be clear but please let me know if you don't get something.
I've created a SQLFiddle as recommended in the comments: http://sqlfiddle.com/#!12/9fd60 It contains the same schema and values as in the images.