Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is another solution to do this: </p> <pre><code>CREATE TABLE TEMP (n int); INSERT INTO Temp VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9); SET @maxid = (SELECT MAX(orders_id) FROM orders); SELECT s.id FROM ( SELECT id FROM ( SELECT t4.n * 1000 + t3.n * 100 + t2.n * 10 + t1.n + 1 AS id FROM TEMP AS t1 CROSS JOIN TEMP AS t2 CROSS JOIN TEMP AS t3 CROSS JOIN TEMP AS t4 ) t WHERE id BETWEEN 1000 AND @maxid ) s LEFT JOIN orders t ON s.id = t.`orders_id` WHERE t.`orders_id` IS NULL; </code></pre> <p>This should give you the <code>orders_id</code>s:</p> <pre><code>ID 1001 1002 1005 1006 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1021 1022 1023 1024 </code></pre> <h2><a href="http://sqlfiddle.com/#!2/fec1a/7" rel="nofollow">SQL Fiddle Demo</a></h2> <p><strong>Note that:</strong> I created a <code>TEMP</code> table with values from 0 to 9, in order to use to generate an anchor table containing values from 1 to 10000 instead of the while loop that you had in the query you posted. You can control the values from this table depending on the values in your table. If you had values that exceed 10000 <code>CROSS JOIN</code> the <code>TEMP</code> table one more time.</p> <p><strong>Update:</strong> If the <code>orders_id</code> is of datatype <code>varchar</code> just cast it to <code>INTEGER</code> like so:</p> <pre><code>SELECT s.id FROM ( SELECT id FROM ( SELECT t4.n * 1000 + t3.n * 100 + t2.n * 10 + t1.n + 1 AS id FROM TEMP AS t1 CROSS JOIN TEMP AS t2 CROSS JOIN TEMP AS t3 CROSS JOIN TEMP AS t4 ) t WHERE id between 1000 AND @maxid ) s LEFT JOIN ( SELECT CONVERT(orders_id, UNSIGNED INTEGER) AS orders_id FROM orders ) t ON s.id = t.`orders_id` WHERE t.`orders_id` IS NULL; </code></pre> <h2><a href="http://sqlfiddle.com/#!2/a798e/6" rel="nofollow">Updated SQL Fiddle Demo</a></h2>
    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