开发者

Query output differs from the expected output

Below query is doing what I need:

SELECT assign.from_uid, assign.aid, assign.message, curriculum.asset, 
       curriculum.title, curriculum.description 
FROM assignment assign 
INNER JOIN curriculum_topics_assets curriculum 
   ON assign.nid = curriculum.asset 
WHERE assign.to_uid = 13 AND assign.status = 1 
GROUP BY assign.from_uid, assign.to_uid, assign.nid 
ORDER BY assign.created DESC

Now I need to get the total count of rows of the result. For example if it is displaying 5 rows the o/p should be like My expected o/p. The query I tried is given below.

SELECT count(description) FROM assignment assign 
INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset 
WHERE assign.to_uid = 13 AND assign.status = 1 
GROUP BY assign.from_uid, assign.to_uid, assign.nid 
ORDER BY assign.created DESC

My expected o/p:

count(*)
---------
5

My current o/p:

count(*)
--开发者_如何学C-------
6
2
5
6
6


The easiest solution would be to

  • place your initial GROUP BY query in a subselect
  • select the amount of rows retrieved from this subselect

SQL Statement

SELECT COUNT(*)
FROM   (
          SELECT  assign.from_uid 
          FROM    assignment assign 
                  INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset 
          WHERE   assign.to_uid = 13 
                  AND assign.status = 1 
          GROUP BY 
                  assign.from_uid
                  , assign.to_uid
                  , assign.nid 
       ) q

Edit - why doesn't the original query return the results required

It did already prepared what was needed to get the correct result

  1. Your query without grouping returns a resultset of 25 records (6+2+5+6+6)
  2. From these 25 records, you have 5 unique combinations of from_uid, to_uid, nid

Now you don't want to count how many records each combination has (as you did in your example) but how many unique (distinct anyone?) combinations there are.

One solution to this is the subselect I presented but following equivalent statement using a DISTINCT clause might be more comprehensive.

SELECT  COUNT(*)
FROM    (
          SELECT  DISTINCT assign.from_uid
                  , assign.to_uid
                  , assign.nid
          FROM    assignment assign 
                  INNER JOIN curriculum_topics_assets curriculum ON assign.nid = curriculum.asset 
          WHERE   assign.to_uid = 13 
                  AND assign.status = 1 
        ) q

Note that my personal preference goes to the GROUP BY solution.


To get the number of rows for a query do:

SELECT COUNT(*) as RowCount FROM (--insert other query here--) s

In you example:

SELECT COUNT(*) as RowCount FROM (SELECT a.from_uid
  FROM assignment a 
  INNER JOIN curriculum_topics_assets c ON a.nid = c.asset 
  WHERE a.to_uid = 13 
    AND a.status = 1 
  GROUP BY a.from_uid, a.to_uid, a.nid 
  ) s

Note that I the dropped the stuff that has no effect on the number of rows to make the query run slightly faster.


You should use COUNT(*) instead of count(description). Look at: http://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜