Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This query:</p> <pre><code> SELECT * FROM notifications n WHERE 1 GROUP BY `from` </code></pre> <p>is simply wrong in ANSI SQL and on almost all DBMS (oracle, postgres, MS SQL etc.).<br> <br> It runs on MySql only because of their nonstandard <code>group by extension</code><br> See this link: <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html" rel="nofollow">http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html</a> <br><br> Hovever they warn about something:</p> <blockquote> <p>However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are <strong>indeterminate</strong>.</p> </blockquote> <p>Because of this "feature" your query (select from select * group by) is unpredicable, results are dependent on the order of records in the table.<br> Take a look at this simple demo: <a href="http://www.sqlfiddle.com/#!2/b762e/2" rel="nofollow">http://www.sqlfiddle.com/#!2/b762e/2</a> <br>There are two identic tables in this demo, with the same content, the only difference is a physical rows order. And the same queries give completely different results.</p> <p><br><br> <strong>---- EDIT how to solve this problem -----</strong></p> <p>To solve this problem in your query, just add both columns to the GROUP BY clause.</p> <pre><code>select * FROM ( SELECT * FROM notifications n GROUP BY `from`, `group_id` ) x WHERE group_id = 'A'; select * FROM ( SELECT * FROM notifications n WHERE group_id = 'A' GROUP BY `from`, `group_id` ) x </code></pre> <p>Above two queries give always the same results for columns <code>from</code> and <code>group_id</code>, other columns (not included in the GROUP BY clause`) can be random.<br> Take a look at simple demo --> <a href="http://www.sqlfiddle.com/#!2/5d19b/5" rel="nofollow">http://www.sqlfiddle.com/#!2/5d19b/5</a></p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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