简体   繁体   中英

How to generate subset in MYSQL?

I have a table letter. One column called 'letter', type is VARCHAR. For example

---------
|letter|
|a     |
|b     |
|c     |
--------

And the output I want is:

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

How to generate the table like that....

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 fiddle: http://sqlfiddle.com/#!9/a8232b9/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM