简体   繁体   English

MySQL COUNT与group_concat

[英]MySQL COUNT With group_concat

I have to query multiple tables in a database to create a list of items, then in order to display the list in pages I have to get the total number of rows using COUNT. 我必须查询数据库中的多个表以创建项目列表,然后为了在页面中显示列表,我必须使用COUNT获取行的总数。

My SQL is as follows: 我的SQL如下:

SELECT
    j.jid,
    j.reference,
    group_concat(COALESCE(p.canview, u.canview)) AS canview
FROM jobs j
LEFT JOIN usergroups u ON (u.gid IN (1,2,3))
LEFT JOIN permissions p ON (p.jid = j.jid) AND (p.gid = u.gid)
GROUP BY j.jid
HAVING FIND_IN_SET('1', canview) > 0
LIMIT 0, 5

Essentially all I want to know is if it's possible, by altering the above query, that I can get the total number of rows returned before the LIMIT . 本质上,我想知道的是,是否有可能通过更改上述查询来获取LIMIT之前返回的总行数。 I've tried added a COUNT(j,jid) but that returns 3 (I think this is the number of rows concatenated with the group_concat ) instead of the desired 9 rows currently in the jobs database that have canview set to 1. 我试过添加一个COUNT(j,jid)但是返回3(我认为这是与group_concat串联的行数),而不是jobs数据库中当前需要将canview设置为1的9行。

Thanks in advance for any help. 在此先感谢您的帮助。

Run the query. 运行查询。

Then run a second query to count the rows: 然后运行第二个查询以计算行数:

select found_rows()

It is documented here . 它记录在这里

EDIT: 编辑:

To get the total without rows, use the SQL_CALC_FOUND_ROWS keyword it the select : 要获得没有行的总数,请使用SQL_CALC_FOUND_ROWS关键字,然后select

select SQL_CALC_FOUND_ROWS . . .

Then found_rows() will return the total without the limit . 然后found_rows()将返回没有limit的总数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM