如何使用 group_concat 和左连接对 mysql 查询的结果进行计数

how to do a count of results of a mysql query with group_concat and left joins

我正在处理一个带有分页的网页,我现在面临的问题之一是它在我创建的过滤器上不起作用。

主要分页是通过 select 使用特定用户 ID 对我的 table 进行计数来工作的。在没有过滤器的情况下效果很好,但是使用过滤器我需要更改此查询以添加内部连接和组连接,因为被过滤的东西在其他 table 中并连接到主 table编号.

我现在有这个过滤器查询:

SELECT DISTINCT games.gameID,games.idUsers,games.gamename,games.comments,games.dateofpurchase,games.lentoutto,games.gameinfo,games.price,games.pictureurl,games.finished,games.rating,games.releasedate, GROUP_CONCAT(DISTINCT labels.labelname ORDER BY labels.labelname) AS labelname, GROUP_CONCAT(DISTINCT category.categoryname ORDER BY category.categoryname) AS categoryname, GROUP_CONCAT(DISTINCT gamemode.gamemode ORDER BY gamemode.gamemode) AS gamemode, GROUP_CONCAT(DISTINCT platform.platform ORDER BY platform.platform) AS platform FROM games LEFT JOIN gamelabels ON gamelabels.gameID=games.gameID LEFT JOIN labels ON labels.labelID=gamelabels.labelID LEFT JOIN categoryconnect ON categoryconnect.gameID=games.gameID LEFT JOIN category ON category.categoryID=categoryconnect.gamescategory LEFT JOIN gamemodesconnect ON gamemodesconnect.gameID=games.gameID LEFT JOIN gamemode ON gamemode.gamemodeID=gamemodesconnect.gamemodeID LEFT JOIN platformconnect ON platformconnect.gameID=games.gameID LEFT JOIN platform ON platform.platformID=platformconnect.platformconnectID WHERE idUsers = '".$userId."' $filter GROUP BY games.gameID ORDER BY gamename

这个过滤器查询非常有效,但是你如何计算这个 returns 的数量或行数?

到目前为止,我尝试的所有操作都让我在 select 部分出现错误。像这样的事情:

SELECT COUNT ( DISTINCT games.gameID,games.idUsers,games.gamename,games.comments,games.dateofpurchase,games.lentoutto,games.gameinfo,games.price,games.pictureurl,games.finished,games.rating,games.releasedate, GROUP_CONCAT(DISTINCT labels.labelname ORDER BY labels.labelname) AS labelname, GROUP_CONCAT(DISTINCT category.categoryname ORDER BY category.categoryname) AS categoryname, GROUP_CONCAT(DISTINCT gamemode.gamemode ORDER BY gamemode.gamemode) AS gamemode, GROUP_CONCAT(DISTINCT platform.platform ORDER BY platform.platform) AS platform) FROM games LEFT JOIN gamelabels ON gamelabels.gameID=games.gameID LEFT JOIN labels ON labels.labelID=gamelabels.labelID LEFT JOIN categoryconnect ON categoryconnect.gameID=games.gameID LEFT JOIN category ON category.categoryID=categoryconnect.gamescategory LEFT JOIN gamemodesconnect ON gamemodesconnect.gameID=games.gameID LEFT JOIN gamemode ON gamemode.gamemodeID=gamemodesconnect.gamemodeID LEFT JOIN platformconnect ON platformconnect.gameID=games.gameID LEFT JOIN platform ON platform.platformID=platformconnect.platformconnectID WHERE idUsers = '".$userId."' $filter GROUP BY games.gameID ORDER BY gamename

我认为您尝试做的是正确的方法,使用子查询。在 MySQL 中,每个子查询 table 都需要一个标识符,它在包裹查询的右括号之后传递,如下所示:

SELECT COUNT(*) FROM (
    SELECT DISTINCT games.gameID,games.idUsers,games.gamename,games.comments,games.dateofpurchase,games.lentoutto,games.gameinfo,games.price,games.pictureurl,games.finished,games.rating,games.releasedate, GROUP_CONCAT(DISTINCT labels.labelname ORDER BY labels.labelname) AS labelname, GROUP_CONCAT(DISTINCT category.categoryname ORDER BY category.categoryname) AS categoryname, GROUP_CONCAT(DISTINCT gamemode.gamemode ORDER BY gamemode.gamemode) AS gamemode, GROUP_CONCAT(DISTINCT platform.platform ORDER BY platform.platform) AS platform FROM games LEFT JOIN gamelabels ON gamelabels.gameID=games.gameID LEFT JOIN labels ON labels.labelID=gamelabels.labelID LEFT JOIN categoryconnect ON categoryconnect.gameID=games.gameID LEFT JOIN category ON category.categoryID=categoryconnect.gamescategory LEFT JOIN gamemodesconnect ON gamemodesconnect.gameID=games.gameID LEFT JOIN gamemode ON gamemode.gamemodeID=gamemodesconnect.gamemodeID LEFT JOIN platformconnect ON platformconnect.gameID=games.gameID LEFT JOIN platform ON platform.platformID=platformconnect.platformconnectID WHERE idUsers = '".$userId."' $filter GROUP BY games.gameID ORDER BY gamename
) count_test;