I have 3 tables
Labels
UserValues
Users
Labels looks like this:
1|First Name
2|Last Name
3|Favorite Book
etc...
Users looks like this:
1|[phone_number]
2|[phone_number]
etc....
UserValues looks like this:
1| [label_id_for_First_Name] |John | [user_id]
2| [label_id_for_Last_Name] |Smith | [user_id]
3| [label_id_for_Fav_Book] |Moby Dick | [user_id]
etc...
some users may have not filled in some fields (all are optional except for the phone number that is used as a primary key).
I’m stumped as to how I can write a query that would flatten this data to look like:
uid |First_Name|Last_Name | Favorite_Book
[user_id]|John |Smith | Moby Dick //user has all fields filled in
[user_id]|Mary | [null] | Kite Runner //user didn't have a last name
The idea is that it would grow in column width for as many columns as there were labels associated with these particular users.
I’d like to select on the users and have 1 row per user with all the values going out to the right.
I can see how I can do this in several queries, but I was hoping to learn what the right way to do this is (maybe it IS to do it in several queries, but I suspect it’s not).
TIA.
If you want to write a query using the current data and giving the result in the example this would be it:
SQL coding horror