Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql force order of results found to match order of IN clause
    primarykey
    data
    text
    <p>This question is different to a <a href="https://stackoverflow.com/questions/4986731/how-to-select-mysql-rows-in-the-order-of-in-clause">commonly asked question</a> about ordering the final results by the IN clause.</p> <p>I would like to force the results returned by the query that contains the IN clause, to match the order of the IN clause.</p> <p><a href="https://stackoverflow.com/questions/5461743/combining-mysql-queries-that-recycle-multiple-self-joins">This is the original question</a> that I am working from.</p> <p>I'd like to alter the query below so that a row containing <code>progress=2</code> occurs before <code>progress=4</code> and <code>progress=7</code> for each <code>session_id</code> when ordering the <code>formation_page_hits</code> table by <code>datetime</code>.</p> <p>Here is the current query:</p> <pre><code>SELECT COUNT(*) FROM ( SELECT session_id FROM formation_page_hits WHERE progress IN (2, 4, 7) AND datetime &gt;= '2011-03-23' AND datetime &lt; '2011-03-24' GROUP BY session_id HAVING COUNT(DISTINCT progress) = 3 ) q </code></pre> <p>These entries</p> <pre><code>datetime, session_id, progress ('2011-03-01 01:02:11', 'abc', 2) ('2011-03-01 01:02:12', 'abc', 4) ('2011-03-01 01:02:13', 'abc', 7) </code></pre> <p>should be a match for the query, but:</p> <pre><code>datetime, session_id, progress ('2011-03-01 01:02:11', 'abc', 4) ('2011-03-01 01:02:12', 'abc', 2) ('2011-03-01 01:02:13', 'abc', 7) </code></pre> <p>should not be a match.</p> <p>Additionally:</p> <pre><code>datetime, session_id, progress ('2011-03-01 01:02:11', 'abc', 4) ('2011-03-01 01:02:12', 'abc', 2) ('2011-03-01 01:02:13', 'abc', 4) ('2011-03-01 01:02:14', 'abc', 7) </code></pre> <p>should be a match.</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.
 

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