0

ex: select 123 from dual; --123!

Result

123
132
213
231
321
312


6 rows return

2

Maybe this:

WITH Nums(Num) AS (
    SELECT 1 UNION SELECT 2 UNION SELECT 3
)
SELECT *
FROM Nums x
CROSS JOIN Nums y
CROSS JOIN Nums z
WHERE x.Num <> y.Num
AND x.Num <> z.Num
AND y.Num <> z.Num
ORDER BY x.Num, y.Num, z.Num

Assumes you are using mysql 8+ with CTEs. Also I have not tested this on mysql, only SQL Server, but hopefully the syntax is the same. Also note your 2nd to last and last results appear to be out of order, unsure if this was intentional.

1

MySQL 5.7:

select concat(a,b,c) result
from   ( 
  select 1 a from dual union select 2 from dual union select 3 from dual
) t1 , (
  select 1 b from dual union select 2 from dual union select 3 from dual
) t2 , (
  select 1 c from dual union select 2 from dual union select 3 from dual
) t3
where t1.a <> t2.b 
  and t2.b <> t3.c 
  and t1.a <> t3.c 
order by a,b,c ;

Output:

+--------+
| result |
+--------+
| 123    |
| 132    |
| 213    |
| 231    |
| 312    |
| 321    |
+--------+
6 rows in set (0.00 sec)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.