首页 > 解决方案 > 我可以使用内部联接改进此查询吗?

问题描述

我有这个查询,它工作正常,但它不是那么容易阅读,我想知道它是否可以以某种方式改进,也许是一个连接。目前,我需要在 assets 表上手动指定除一列(特别是 id_template)之外的所有列,但我宁愿直接在 id 字段的顶部加入子查询。我不知道该怎么做,这甚至可能吗?

SELECT *, (
    SELECT to_json(template) AS template
    FROM (
        SELECT *, (
            SELECT json_agg(to_json(fields)) as fields
            FROM (
                SELECT *
                FROM public.fields
                WHERE id_template = templates.id
            ) fields
        )
        FROM public.templates    
        WHERE templates.id = assets.id_template
    ) template
)
FROM public.assets
WHERE assets.id = $1

这里请求的是上述查询返回的JSON格式的数据,请原谅名称怪异的测试数据等。

[
 {
   "name": "egnf",
   "data": "{}",
   "id": "8358b1f0-3935-4104-92a5-0c6efdecc747",
   "id_organisation": "b963890b-d956-48d3-8fa4-2f6d70d87bd7",
   "id_template": "7958b0ef-f9cf-4861-b478-40072ca58ad3",
   "created_date": "2019-01-24 16:20:53.744976+00",
   "last_modified_date": "2019-01-28 16:47:59.108746+00",
   "last_modified_by": "3138ced1-33be-4969-94f8-5b63914452d5",
   "template":     {  
       'name':'mod test',
       'data':{  

       },
       'type':'asset',
       'id_organisation':'b963890b-d956-48d3-8fa4-2f6d70d87bd7',
       'id':'7958b0ef-f9cf-4861-b478-40072ca58ad3',
       'last_modified_date':'2019-01-22T17:13:57.973411+00:00',
       'last_modified_by':'3138ced1-33be-4969-94f8-5b63914452d5',
       'created_date':'2019-01-15T14:17:49.831689+00:00',
       'fields':[  
          {  
             'name':'test field',
             'data':None,
             'configuration':'',
             'id_organisation':'b963890b-d956-48d3-8fa4-2f6d70d87bd7',
             'id_template':'7958b0ef-f9cf-4861-b478-40072ca58ad3',
             'id_format':'85b89541-0c57-4bb2-97e8-86fc4efbc568',
             'id':'e5f16e97-2843-4383-80ef-08baf6b04311',
             'last_modified_date':'2019-01-22T17:13:58.446235+00:00',
             'last_modified_by':'3138ced1-33be-4969-94f8-5b63914452d5',
             'created_date':'2019-01-21T12:58:15.821335+00:00'
          },
          {  
             'name':'third field',
             'data':None,
             'configuration':'',
             'id_organisation':'b963890b-d956-48d3-8fa4-2f6d70d87bd7',
             'id_template':'7958b0ef-f9cf-4861-b478-40072ca58ad3',
             'id_format':'66276b7f-64bb-4116-8dfc-28a300d0e34b',
             'id':'1a961240-98ec-45e0-93b2-099ce48cc521',
             'last_modified_date':'2019-01-22T17:13:58.447973+00:00',
             'last_modified_by':'3138ced1-33be-4969-94f8-5b63914452d5',
             'created_date':'2019-01-21T14:14:29.235838+00:00'
          }
       ]
    }
 }
]

谢谢!

标签: postgresql

解决方案


看起来有点棘手。我自己对postgres还很陌生。由于没有任何数据可以测试我的查询,我必须像这样提交它。

您的查询包含一些所谓的相关子查询,即当select外部查询的 - 语句中有子查询时,它们通过 - 子句链接where。据我所知,它们可以用左连接代替。所以这就是我所做的。希望它有效。如果没有,也许它仍然给了你一个新的想法。

SELECT assets.*, templates.* , json_agg(to_json(fields)) as field
FROM public.assets
LEFT JOIN public.templates  
ON templates.id = assets.id_template
LEFT JOIN public.fields
ON fields.id_template = templates.id
WHERE assets.id = $1
GROUP BY (all variables till json_agg);

推荐阅读