Home > database >  Is there a way to make the numbers run in the correct order?
Is there a way to make the numbers run in the correct order?

Time:06-17

So when i run this query i created the numbers come back in the wrong order and i cannot figure out why

SELECT ones.num   tens.num   1
FROM
 (SELECT 0 num UNION ALL
  SELECT 1 num UNION ALL
  SELECT 2 num UNION ALL
  SELECT 3 num UNION ALL
  SELECT 4 num UNION ALL
  SELECT 5 num UNION ALL
  SELECT 6 num UNION ALL
  SELECT 7 num UNION ALL
  SELECT 8 num UNION ALL
  SELECT 9 num
 ) ones
CROSS JOIN
 (SELECT 0 num UNION ALL
  SELECT 10 num UNION ALL
  SELECT 20 num UNION ALL
  SELECT 30 num UNION ALL
  SELECT 40 num UNION ALL
  SELECT 50 num UNION ALL
  SELECT 60 num UNION ALL
  SELECT 70 num UNION ALL
  SELECT 80 num UNION ALL
  SELECT 90 num
 ) tens;

CodePudding user response:

It pretty much seems to come in order, however to remove any confusion why don't you wrap it all as a subquery and use an order by clause:

SELECT * FROM (SELECT ones.num   tens.num   1 number
FROM
 (SELECT 0 num UNION ALL
  SELECT 1 num UNION ALL
  SELECT 2 num UNION ALL
  SELECT 3 num UNION ALL
  SELECT 4 num UNION ALL
  SELECT 5 num UNION ALL
  SELECT 6 num UNION ALL
  SELECT 7 num UNION ALL
  SELECT 8 num UNION ALL
  SELECT 9 num
 ) ones
CROSS JOIN
 (SELECT 0 num UNION ALL
  SELECT 10 num UNION ALL
  SELECT 20 num UNION ALL
  SELECT 30 num UNION ALL
  SELECT 40 num UNION ALL
  SELECT 50 num UNION ALL
  SELECT 60 num UNION ALL
  SELECT 70 num UNION ALL
  SELECT 80 num UNION ALL
  SELECT 90 num
 ) tens) final ORDER BY number DESC;
  • Related