【问题标题】:Retrieve count of the result set and other columns in a single query在单个查询中检索结果集和其他列的计数
【发布时间】:2016-10-21 11:25:10
【问题描述】:

我有以下查询对contentstag 表进行内部联接。 (使用mysql)

            SELECT
                contents.original_duration,
                tag.tagconfig,
                contents.id_contents
            FROM
                contents
                    inner JOIN
                tag
                    ON
                contents.id_contents = tag.id_contents
                AND contents.id_host = tag.id_host
                AND contents.id_vhost = tag.id_vhost

            WHERE
                tag.video_insert_status='idle'
                AND contents.type = 'video'
                AND contents.subtype = 'video'
                AND original_duration is not NULL;

id_contentsid_hostid_vhost 是机器人 tagcontents 表中的主键。以上查询结果260条。

如何在单个查询中选择此计数?

我试过了

SELECT
                contents.original_duration,
                tag.tagconfig,
                contents.id_contents,
                count(contents.original_duration)

但是,它似乎没有给出正确的结果。

【问题讨论】:

  • 您希望将结果作为新列还是其他内容?

标签: mysql sql


【解决方案1】:

您可以使用的一个技巧是 GROUP BY 选择列表中的所有列,然后使用 COUNT(*) 获取记录数:

SELECT
    contents.original_duration,
    tag.tagconfig,
    contents.id_contents,
    COUNT(*) AS recordCount
FROM
    contents
        inner JOIN
    tag
        ON
    contents.id_contents = tag.id_contents
    AND contents.id_host = tag.id_host
    AND contents.id_vhost = tag.id_vhost
WHERE
    tag.video_insert_status='idle'
    AND contents.type = 'video'
    AND contents.subtype = 'video'
    AND original_duration is not NULL;
GROUP BY
    contents.original_duration,
    tag.tagconfig,
    contents.id_contents

【讨论】:

    【解决方案2】:

    将原始查询的 SELECT 子句替换为

    SELECT COUNT(*)
    

    将给出计数。

    【讨论】:

      【解决方案3】:

      我认为您可以使用两个相同的查询并将它们加入以获得您想要的内容,例如:

      SELECT T1.*, T2.CNT
      FROM (
          SELECT
              contents.original_duration,
              tag.tagconfig,
              contents.id_contents
          FROM
              contents
                  inner JOIN
              tag
                  ON
              contents.id_contents = tag.id_contents
              AND contents.id_host = tag.id_host
              AND contents.id_vhost = tag.id_vhost
      
          WHERE
              tag.video_insert_status='idle'
              AND contents.type = 'video'
              AND contents.subtype = 'video'
              AND original_duration is not NULL) T1
      INNER JOIN (
          SELECT
              COUNT(*) AS CNT
          FROM
              contents
                  inner JOIN
              tag
                  ON
              contents.id_contents = tag.id_contents
              AND contents.id_host = tag.id_host
              AND contents.id_vhost = tag.id_vhost
      
          WHERE
              tag.video_insert_status='idle'
              AND contents.type = 'video'
              AND contents.subtype = 'video'
              AND original_duration is not NULL
      ) T2
      

      或者你可以执行下面的sql,你可以通过最后一条记录来计数:

      SELECT
          contents.original_duration,
          tag.tagconfig,
          contents.id_contents,
          @CNT := @CNT + 1 AS CNT
      FROM
          contents
              inner JOIN
          tag
              ON
          contents.id_contents = tag.id_contents
          AND contents.id_host = tag.id_host
          AND contents.id_vhost = tag.id_vhost
          CROSS JOIN (SELECT @CNT := 0) T
      WHERE
          tag.video_insert_status='idle'
          AND contents.type = 'video'
          AND contents.subtype = 'video'
          AND original_duration is not NULL
      ORDER BY CNT
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多