Mysql 是否可以合并这些SQL select语句?

Mysql 是否可以合并这些SQL select语句?,mysql,sql,Mysql,Sql,我正在使用mysql/mariadb 我想做的是:获取每个组的id,名称,私有/然后获取每个组中视频的数量/然后获取每个组的最后20个视频 现在,我使用三个单独的SQL查询,然后每次针对每个id运行它。有没有办法将三个SQL查询组合成一个查询,并在id列表上运行它们 例如,在组1、2、3上: Executing (default): select `id`, `name`, `private` from `Groups` where `id` = '1' limit 1 Executing (d

我正在使用mysql/mariadb

我想做的是:获取每个
组的
id
名称
私有
/然后获取每个
组中
视频
的数量
/然后获取每个
组的最后20个
视频

现在,我使用三个单独的SQL查询,然后每次针对每个id运行它。有没有办法将三个SQL查询组合成一个查询,并在id列表上运行它们

例如,在组1、2、3上:

Executing (default): select `id`, `name`, `private` from `Groups` where `id` = '1' limit 1
Executing (default): select `id`, `name`, `private` from `Groups` where `id` = '2' limit 1
Executing (default): select `id`, `name`, `private` from `Groups` where `id` = '3' limit 1

Executing (default): select count(*) from `Videos` where `GroupId` = '1' and `live` = true
Executing (default): select count(*) from `Videos` where `GroupId` = '2' and `live` = true
Executing (default): select count(*) from `Videos` where `GroupId` = '3' and `live` = true

Executing (default): select `id`, `file` from `Videos` where `GroupId` = '1' and `live` = true order by `id` desc limit 20
Executing (default): select `id`, `file` from `Videos` where `GroupId` = '2' and `live` = true order by `id` desc limit 20
Executing (default): select `id`, `file` from `Videos` where `GroupId` = '3' and `live` = true order by `id` desc limit 20

Executing (default): (SELECT `id`, `name`, `private` FROM `Groups` WHERE`id` = '1' LIMIT 1)
                     UNION ALL
                     (SELECT `id`, `name`, `private` FROM `Groups` WHERE`id` = '2' LIMIT 1)
                     UNION ALL
                     (SELECT `id`, `name`, `private` FROM `Groups` WHERE`id` = '3' LIMIT 1)

Executing (default): SELECT count(*) 
                     FROM `Videos` 
                     WHERE `GroupId` IN ('1', '2', '3')  AND `live` = true

Executing (default): (SELECT `id`, `file` FROM `Videos` WHERE`GroupId` = '1' AND `live` = true ORDER BY `id` DESC LIMIT 20)
                     UNION ALL
                     (SELECT `id`, `file` FROM `Videos` WHERE`GroupId` = '2' AND `live` = true ORDER BY `id` DESC LIMIT 20)
                     UNION ALL
                     (SELECT `id`, `file` FROM `Videos` WHERE`GroupId` = '3' AND `live` = true ORDER BY `id` DESC LIMIT 20)

我是否可以有效地将三个查询分解为一个或两个查询,或者这是获得必要数据所需的最小查询数?取决于表之间是否有链接以及您希望数据如何输出它只给出第一条记录1/2/3,但提问者需要所有三条记录来返回所需结果