Note that there are some explanatory texts on larger screens.

plurals
  1. POSomething strange in mysql query because of Group By Statement
    primarykey
    data
    text
    <p>I have this table which recording exam results for the users:</p> <pre><code>CREATE TABLE IF NOT EXISTS `ts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL, `xid` int(11) NOT NULL, `lid` int(11) NOT NULL, `grade` double NOT NULL, `duration` varchar(10) COLLATE utf8_turkish_ci NOT NULL, `trues` int(11) NOT NULL DEFAULT '0', `falses` int(11) NOT NULL DEFAULT '0', `empties` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `uid` (`uid`), KEY `xid` (`xid`), KEY `lid` (`lid`), ) ENGINE=InnoDB; </code></pre> <p>And I have this records in the table:</p> <pre><code>|id|uid|xid|lid|grade|duration|trues|falses|empties| +--+---+---+---+-----+--------+-----+------+-------+ |45| 20| 1| 1| 80|00:01:12| 8| 2| 0| |46| 20| 1| 2| 40|00:02:31| 4| 6| 0| |47| 20| 3| 1| 100|00:01:10| 1| 0| 0| |48| 20| 4| 1| 83|00:00:51| 5| 1| 0| |49| 20| 2| 1| 0|00:00:02| 0| 1| 0| |53| 20| 4| 2| 50|00:00:04| 1| 1| 0| |54| 20| 1| 2| 50|00:00:41| 5| 5| 0| </code></pre> <p><code>lid</code> indicates the difficulty level of exam which is represented by <code>xid</code>. I want to show the highest grades for each unique exams with unique levels. For example there are 7 rows. and id[46] and id[54] are for the same exams and levels. So I must list the higher grade value among these two rows.</p> <p>I have made a query:</p> <pre><code>SELECT failed.fgrade, succeeded.sgrade, ts.xid, ts.lid, ts.trues, ts.falses, ts.empties, MAX(`grade`) AS grade, ts.duration, x.exam FROM ts JOIN x ON ts.xid = x.id JOIN (SELECT COUNT(DISTINCT(xid)) AS sgrade FROM ts WHERE uid = 20 AND grade &gt;= 70) AS succeeded JOIN (SELECT COUNT(DISTINCT(xid)) AS fgrade FROM ts WHERE uid = 20 AND grade &lt; 70 ) AS failed WHERE ts.uid = 20 GROUP BY xid, lid ORDER BY ts.id </code></pre> <p>When I run the query I get strange result:</p> <pre><code>Exam Name Level Duration True False Empty Total Grade fgrade sgrade Functions 1 00:00:02 0 1 0 1 0 3 3 Numbers 2 00:02:31 4 6 0 10 50 3 3 (!) Equations 2 00:00:04 1 1 0 2 50 3 3 Numbers 1 00:01:12 8 2 0 10 80 3 3 Equations 1 00:00:51 5 1 0 6 83 3 3 Functions 1 00:01:10 1 0 0 1 100 3 3 </code></pre> <p>If you look at the (!) row in the result table there is something weird. 4 correct answers and 6 false answers make 40 points. But it shows the other row's grade value while showing own true and false answer counts.</p> <p>I am basically wishing to show the all different exams for each difficulty levels for a given userid (<code>uid</code>). Now as I am writing this question, I am starting to think that it may be due to the <code>GROUP BY</code> statement. But it has taken a while to write the question. I cannot delete this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload