Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an indirect solution to get the sequence as per the requirement.</p> <p>As i was using PHP to fetch data, first i wrote an sql query to fetch the data according to priority.</p> <pre><code>SELECT `Category`.`id`, `Category`.`name`, `Category`.`priority` , `Product`.`name`, `Product`.`category_id` FROM `categories` AS `Category` INNER JOIN `products` AS `Product` WHERE `Category`.`id` = `Product`.`category_id` ORDER BY CASE WHEN `Category`.`priority` IS NULL THEN 1 ELSE 0 END ASC, `Category`.`priority` ASC; </code></pre> <p>Then i did a simple array manipulation to get an array as follow.</p> <pre><code>array( (int) 1 =&gt; array( (int) 0 =&gt; array( (int) 0 =&gt; '1', (int) 1 =&gt; '6' ), (int) 1 =&gt; array( (int) 0 =&gt; '2', (int) 1 =&gt; '7' ), (int) 2 =&gt; array( (int) 0 =&gt; '3', (int) 1 =&gt; '8' ), (int) 3 =&gt; array( (int) 0 =&gt; '4' ), (int) 4 =&gt; array( (int) 0 =&gt; '5' ), ), (int) 2 =&gt; array( (int) 0 =&gt; array( (int) 0 =&gt; '9' ), (int) 1 =&gt; array( (int) 0 =&gt; '10' ), (int) 2 =&gt; array( (int) 0 =&gt; '11' ), ), '' =&gt; array( (int) 0 =&gt; array( (int) 0 =&gt; '12' ), (int) 1 =&gt; array( (int) 0 =&gt; '13' ) ) ) </code></pre> <p>for series c1p1, c2p1, c1p2, c2p2, c1p3, c2p3, c1p4, c1p5, c3p1, c3p2, c3p3, c4p1, c4p2. Where array keys are priority and leaf values are product ids.</p> <p>Then i make a string of ids from the above array as follows.</p> <pre><code> 1,6,2,7,3,8,4,5,9,10,11,12,13 </code></pre> <p>And finally wrote the query as follows.</p> <pre><code> SELECT * FROM `products` WHERE `id` IN ( 1, 6, 2, 7, 3, 8, 4, 5, 9, 10, 11, 12, 13 ) ORDER BY FIELD( id, 1, 6, 2, 7, 3, 8, 4, 5, 9, 10, 11, 12, 13 ); </code></pre>
 

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