如何在MYSQL中生成子集?

时间:2017-11-26 23:48:31

标签: mysql

我有一封表格信。一列名为'letter',类型为VARCHAR。 例如

-----------------
|NULL|NULL|NULL|
|a   |NULL|NULL|
|b   |NULL|NULL|
|c   |NULL|NULL|
|a   |b   |NULL|
|a   |c   |NULL|
|b   |c   |NULL|
|a   |b   |c   |
-----------------

我想要的输出是:

return

如何像那样生成表....

1 个答案:

答案 0 :(得分:0)

CREATE TABLE letters (letter char);
INSERT INTO letters VALUES ('a');
INSERT INTO letters VALUES ('b');
INSERT INTO letters VALUES ('c');

SELECT
   t1.letter,
   t2.letter,
   t3.letter
 FROM 
   (SELECT NULL AS letter UNION SELECT letter FROM letters) t1
   JOIN (SELECT NULL AS letter UNION SELECT letter FROM letters) t2 ON t2.LETTER IS NULL OR t2.letter > t1.letter
   JOIN (SELECT NULL AS letter UNION SELECT letter FROM letters) t3 ON t3.LETTER IS NULL OR t3.letter > t2.letter

SQL小提琴:http://sqlfiddle.com/#!9/a8232b9/2