Getting total mysql results returned from query with a limit: FOUND_ROWS error

时间秒杀一切 提交于 2020-01-16 07:52:51

问题


I have this mysql query:

SELECT Files.GUID, Files.Name, Files.Type, Files.Visibility, Files.CreationDate, Files.OwnerUser, Date, BackupsCount
FROM Files
  LEFT JOIN (
    SELECT FileGUID, Date, COUNT(*) AS BackupsCount
    FROM Versions
    GROUP BY FileGUID
  ) Versions ON Files.GUID = Versions.FileGUID
WHERE Files.ParentFolder = '96251A8B-B2A8-416B-92D7-3509E6A645C7'
ORDER BY Files.Type DESC, CreationDate ASC
LIMIT 0, 50

I'd like to know how many record this query would return if the LIMIT directive was not there, I googled and found that I needed to add SELECT SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS();

MY problem is: I couldn't get it to work:

SELECT SQL_CALC_FOUND_ROWS * FROM (
SELECT Files.GUID, Files.Name, Files.Type, Files.Visibility, Files.CreationDate, Files.OwnerUser, Date, BackupsCount
FROM Files
  LEFT JOIN (
    SELECT FileGUID, Date, COUNT(*) AS BackupsCount
    FROM Versions
    GROUP BY FileGUID
  ) Versions ON Files.GUID = Versions.FileGUID
WHERE Files.ParentFolder = '96251A8B-B2A8-416B-92D7-3509E6A645C7'
ORDER BY Files.Type DESC, CreationDate ASC
LIMIT 0, 50)

-- Find total rows
SELECT FOUND_ROWS()

Assuming this is good idea (I mean doing one query vs two separate ones), how can I get the FOUND_ROWS() to work with this query?


回答1:


The idea is correct, and the second query is almost instantaneous. But you have to add the keyword after the SELECT, no more:

SELECT Files.GUID, Files.Name, Files.Type, ...

becomes

SELECT SQL_CALC_FOUND_ROWS Files.GUID, Files.Name, Files.Type, ...

Then the number of found rows will be populated and you'll be able to recover it.

If you want to run a single retrieval query, but you are able to execute more than one query, then you can do this (it is two queries):

SELECT COUNT(*) INTO @FOUNDROWS FROM ( YOUR_QUERY_WITHOUT_SELECT ) AS orig;

and alter your retrieval query by adding @FOUNDROWS as foundrows to the fields:

SELECT ...YOUR FIELDS..., @FOUNDROWS AS foundrows FROM...

Finally you can run a JOIN as a single query containing the two queries:

SELECT ALL_YOUR_FIELDS, counter.FOUND_ROW_NUMBER FROM
( YOUR_QUERY_WITH_LIMIT ) AS limited
JOIN ( SELECT COUNT(*) AS FOUND_ROW_NUMBER FROM YOUR_QUERY ) AS counter;



回答2:


The query should be like this:

SELECT SQL_CALC_FOUND_ROWS Files.GUID, Files.Name, Files.Type, Files.Visibility, Files.CreationDate, Files.OwnerUser, Date, BackupsCount
FROM Files
  LEFT JOIN (
    SELECT FileGUID, Date, COUNT(*) AS BackupsCount
    FROM Versions
    GROUP BY FileGUID
  ) Versions ON Files.GUID = Versions.FileGUID
WHERE Files.ParentFolder = '96251A8B-B2A8-416B-92D7-3509E6A645C7'
ORDER BY Files.Type DESC, CreationDate ASC
LIMIT 0, 50



回答3:


A very simple example


 "SELECT SUM(final_cost) as final_cost,
SUM(call_discount) as discount_cost,
(SELECT COUNT(*) FROM call_records WHERE transaction_type ='call' AND account_id='$account_id' AND time_of_call>='$from' AND time_of_call<='$to' ) as calls,
(SELECT COUNT(*) FROM call_records WHERE transaction_type ='text' AND account_id='$account_id' AND time_of_call>='$from' AND time_of_call<='$to' ) as texts,
(SELECT COUNT(*) FROM call_records WHERE transaction_type ='appointment' AND account_id='$account_id' AND time_of_call>='$from' AND time_of_call<='$to' ) as appts
 FROM
 call_records
 WHERE account_id='$account_id'
 AND time_of_call>='$from'
 AND time_of_call<='$to' "


来源:https://stackoverflow.com/questions/12915374/getting-total-mysql-results-returned-from-query-with-a-limit-found-rows-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!