How to use joins to get this data
Options
Kevin Wasie
Member ✭
I have three tables:
property
• id
• address
properties_to_users
• id
• user_id
• property_id
• role_name
user
• id
• name
How do I get the result of a query that shows the properties associated with a user with the users roles. I only want to show the property once.
This is for an authenticated user, so the input is the user_id.
It should look like this:
user_properties = [
{
id: 001
address: "1234 main st"
user_roles: [ "editor","admin","common" ]
},
{
id: 004
address: "59 Halifax Rd"
user_roles: [ "editor","common" ]
},
{
id: 008
address: "50 MLK Jr. Blvd"
user_roles: [ "common","admin" ]
}
]
I can figure out how to join and show the property multiple times, with the associated role. But, I cannot figure out how to only show the property once, with the associated roles.
property
• id
• address
properties_to_users
• id
• user_id
• property_id
• role_name
user
• id
• name
How do I get the result of a query that shows the properties associated with a user with the users roles. I only want to show the property once.
This is for an authenticated user, so the input is the user_id.
It should look like this:
user_properties = [
{
id: 001
address: "1234 main st"
user_roles: [ "editor","admin","common" ]
},
{
id: 004
address: "59 Halifax Rd"
user_roles: [ "editor","common" ]
},
{
id: 008
address: "50 MLK Jr. Blvd"
user_roles: [ "common","admin" ]
}
]
I can figure out how to join and show the property multiple times, with the associated role. But, I cannot figure out how to only show the property once, with the associated roles.
Comments
-
This would be a query all records on properties.
Then you would add a join to properties_to_users and link property.id to property_to_users.property_id within the join block.
Last in the main block you can add property_to_users.user_id = auth id -
how do you get the array of roles inside the property?
The user can have several roles associated with the property.
I've tried addon's and evals but cannot get it. -
I would loop over the data in the function stack to create a new, summarized list that you can then pass back through your output. You already have the data you need coming out of the query. Looping over and reassembling into the more concise list is a bit of work but straightforward. I've helped other users do this this kind of reshaping in my sessions. I would estimate maybe 30-45 minutes to get that done and have you understanding the process so you can apply it in the future. (Which you will - transforming data is super-useful!)
-
Awesome! Thank you. I'll give that a shot.
Categories
- All Categories
- 53 ? Announcements
- 47 ? Releases
- 37 ? Welcome
- 983 ? Help! I'm a Noob
- 125 ? No-Code Front-Ends
- 633 ? Working with APIs
- 439 ? Transforming data
- 126 ? Connect Xano to ...
- 50 ?? Find an Expert
- 348 ❓Other questions
- 35 ? Security
- 22 ✂️ Snippets
- 19 ? Showcase
- 7 ?️ Xano Chatter
- 62 ? Video Tutorials
- 171 ? Request a feature
- 229 ? Report a Bug
- 19 ? Templates & Extensions
- 7 ? Feedback