0

我正在尝试列出唯一user engagementsengagement_type

我曾经有以下查询。

SELECT u.id, u.fname, u.lname FROM (
  SELECT id, engagement_type FROM (
    SELECT user_id AS id, 'comment'
    FROM comments WHERE commentable_id = 48136 AND commentable_type = 'Video'
    UNION ALL
    SELECT user_id AS id, 'like'
    FROM likes WHERE likeable_id = 48136 AND likeable_type = 'Video'
  ) AS a
  GROUP BY id
  LIMIT 10
) b JOIN users u USING (id);

回报:

id        | fname      | lname
------------------------------
1         | joe        | abc
2         | sarah      | qer
3         | megan      | tryey
4         | john       | vdfa

这很好。现在,我想包括参与类型。我想出了这个:

SELECT u.id, u.fname, u.lname, engagement_type FROM (
  SELECT id, engagement_type FROM (
    SELECT user_id AS id, 'comment' AS engagement_type
    FROM comments WHERE commentable_id = 48136 AND commentable_type = 'Video'
    UNION ALL
    SELECT user_id AS id, 'like' AS engagement_type FROM likes
    WHERE likeable_id = 48136 AND likeable_type = 'Video'
  ) AS a
  GROUP BY id, engagement_type
  LIMIT 10
) b JOIN users u USING (id);

现在返回:

id        | fname      | lname    | engagement_type
---------------------------------------------------
1         | joe        | abc      | comment
2         | sarah      | qer      | like
3         | megan      | tryey    | like
4         | john       | vdfa     | like
1         | joe        | abc      | like     
3         | megan      | tryey    | comment

上面的唯一问题。结果不再是唯一的。如您所见,Joe 和 Megan 有 2 个条目。

知道我怎样才能让它工作吗?

4

2 回答 2

1

DISTINCT ON在 PostgreSQL 中更简单。
由于缺乏定义,我engagement_type根据其排序顺序选择第一个:

SELECT u.id, u.fname, u.lname, b.engagement_type
FROM  (
   SELECT DISTINCT ON (1)
          id, engagement_type
   FROM (
      SELECT user_id AS id, 'comment' AS engagement_type
      FROM   comments
      WHERE  commentable_id = 48136
      AND    commentable_type = 'Video'

      UNION ALL
      SELECT user_id, 'like'
      FROM   likes
      WHERE  likeable_id = 48136 
      AND    likeable_type = 'Video'
      ) a
   ORDER  BY 1, 2
   LIMIT  10
   ) b
JOIN users u USING (id);

详细信息、链接和说明:

如果您想要所有参与类型的唯一列表

  SELECT id, string_agg(DISTINCT engagement_type, ', ') AS engagement_types
   FROM (
      ...
      ) a
   GROUP  BY 1
   ORDER  BY <whatever>
   LIMIT  10;

string_agg()需要 Postgres 9.0 或更高版本。
此表单允许按您想要的任何顺序进行排序,而如果您ORDER BY不同意,则需要另一个子查询DISTINCT ON

于 2013-03-03T18:28:37.003 回答
0

如果您只想要一行,则需要从 GROUP BY 子句中删除engagement_type。但是,这不会向您显示所有不同的engagement_type。

如果您想在一行中列出engagement_types 而不复制用户详细信息,请使用 ARRAY_TO_STRING 函数。这会将结果污染到一行。因此,您可以将评论表中的参与类型列为逗号分隔列表。

于 2013-03-03T16:08:25.123 回答