【问题标题】:convert postgres query to django query将 postgres 查询转换为 django 查询
【发布时间】:2016-10-23 07:36:44
【问题描述】:

我需要有关此查询的帮助。

SELECT  s.nombre as 'habilidad',
        GROUP_CONCAT( a.carrera_id )
FROM    perfiles_alumno a,
        perfiles_alumno_habilidades h,
        perfiles_universidad u,
        segmentacion_habilidad s
WHERE   u.user_id = %s
AND     a.universidad_id = u.id
AND     a.id = h.alumno_id
AND     s.id = h.habilidad_id
GROUP BY h.habilidad_id
ORDER BY a.carrera_id

我需要在 Django 中执行此查询。我可以在运行 SQLite3 的本地环境中使用它,但是当我推送到生产环境时它会失败,因为我有 Postgres。

有人可以帮我吗?

【问题讨论】:

    标签: python django postgresql sqlite


    【解决方案1】:

    要将此 SQL 转换为 Postgres,请将 group_concat 更改为 string_agg(s.carrera_id, ',')。但随后它将不再适用于 SQLite。如果您在开发和生产中使用相同的数据库,您可能会遭受更少的痛苦。

    如果你想知道每个组的s.nombre,我认为你需要GROUP BY h.habilidad_id, s.nombre

    此外,您应该避免使用%s 插入用户提供的值,因为这存在安全风险。

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多