Question

Which is fastest/most efficient? Both work.

Solution 1. Grouping by the id:

SELECT
    *, 
    COUNT(*) AS pages 
FROM
    notis 
WHERE
    cid = 20 
GROUP BY
    n.id
ORDER BY
    nid DESC 

Solution 2. Count in a subquery:

SELECT
    *,
    (select count(*) FROM notis WHERE cid=20) AS count
FROM
    notis
WHERE
    cid = 20
ORDER BY
    nid DESC

No correct solution

OTHER TIPS

enter image description here

Solution 1 is Best and fast: because solution 2 have subquery

BUT you have to change your query like that:

  SELECT *, COUNT(*) AS pages 
  FROM notis 
  WHERE cid = 20 
  GROUP BY nid
  ORDER BY nid DESC 

you changed the place of GROUP BY and ORDER BY.

Idont know if you looking for count(*) or you want count by every nid . if count(*) then consider to not use GROUP BY

 SELECT *, COUNT(*) AS pages 
 FROM notis 
 WHERE cid = 20 
 ORDER BY nid DESC 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top