在单个查询中检索结果集和其他列的计数

时间:2016-06-20 09:09:42

标签: mysql sql

我有以下查询对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)

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

3 个答案:

答案 0 :(得分:0)

您可以使用的一个技巧是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

答案 1 :(得分:0)

替换原始查询的SELECT子句
SELECT COUNT(*)

将给予计数。

答案 2 :(得分:0)

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

let ref = firebase.child("users").child(fUID).child("participating")

ref.observe(.value, with: { (snapshot: FIRDataSnapshot!) in
    print(snapshot.childrenCount)

    let room = Room()
    // Use you snapshot(FIRDataSnapshot) to create the data of the room.
    rooms.append(room)
})

return rooms.count

或者您可以执行以下sql,您可以按最后一条记录计算:

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