title: GROUP BY only primary key, but select other values
body: <p>Is there a way to group by a unique (primary) key, essentially giving an implicit guarantee that the other columns from that table will be well-defined?</p>
  
  <pre><code>SELECT myPrimaryKey, otherThing
  FROM myTable
  GROUP BY myPrimaryKey
  </code></pre>
  
  <p>I know that I can add the other columns to the statement (<code>GROUP BY myPrimaryKey,otherThing</code>), but I'm trying to avoid that. If you're curious why, read on:</p>
  
  <hr>
  
  <p>I have a statement which is essentially doing this:</p>
  
  <pre><code>SELECT nodes.node_id, nodes.node_label, COUNT(1)
  FROM {a couple of joined tables}
  INNER JOIN nodes USING (node_id)
  GROUP BY nodes.node_id, nodes.node_label
  </code></pre>
  
  <p>which works fine, but is a bit slow in MySQL. If I remove <code>nodes.node_label</code> from the <code>GROUP BY</code>, it runs about 10x faster (according to <code>EXPLAIN</code>, this is because one of the earlier joins starts using an index when previously it didn't).</p>
  
  <p>We're in the process of migrating to Postgres, so all new statements are supposed to be compatible with <em>both</em> MySQL <em>and</em> Postgres when possible. Now in Postgres, the original statement runs fast, but the new statement (with the reduced group by) won't run (because Postgres is stricter). In this case, it's a false error because the statement is actually well-defined.</p>
  
  <p>Is there a syntax I can use which will let the same statement run in both platforms, while letting MySQL use just one column in the group by for speed?</p>