Skip to content
Advertisement

How to keep table name when inner joining related tables

I am new to SQL and wonder how to select nested tables. I have two tables like this:

sensors

sensor_id model_no location_id
int varchar int

locations

location_id name location radius
int varchar point int

They are linked with foreign key. Currently, I select using

to get the data from both like this:

I wonder if there is any way I can keep the location data grouped as its own object like this:

I am using MySQL 8 with mysql npm package to execute the queries. I know I can modify the response using javascript but wonder if it can be done directly in the query, and if so, is it better or worse for performance?

Advertisement

Answer

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b17dfa3069b4bb9345a9e99e8b893121

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement