N
N
Neuro2019-07-01 13:37:37
SQL
Neuro, 2019-07-01 13:37:37

How to put the result of an sql query into an object?

Hello.
The crux of the matter, I'm doing a sql query, here it is

SELECT  sensor_alerts.*, sensor_instances.description, 
          sensor_instances.lastupdated, groups.address, 
          groups.city, groups.country
      FROM sensor_alerts
      LEFT JOIN sensor_instances ON sensor_instances.id = sensor_alerts.sensorid 
      LEFT JOIN groups ON sensor_alerts.groupid = groups.parentid

I get back an object with string properties of this type
{
  description: 1321231231
  city: asdad
  addres: asgs
  country: sфывфы
}

how can i have the properties groups.address, groups.country, and groups.city be placed on the groups object?
what would happen like this
{
  description: 1321231231
  groups: {
    city: asdad
    addres: asgs
    country: sфывфы
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey P, 2019-07-01
@Riveran

SELECT 
    sensor_alerts.*,
    sensor_instances.*,
    json_agg(groups.*) as groups
FROM sensor_alerts
      LEFT JOIN sensor_instances ON sensor_instances.id = sensor_alerts.sensorid 
      LEFT JOIN groups ON sensor_alerts.groupid = groups.parentid
GROUP BY sensor_alerts.id; --- скорее всего группировка не обязательна

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question