0

I have this table, I am trying to figure out a way to get the unique values from both the columns.

CREATE TABLE `test` (
id bigint(20) NOT NULL,
col1 varchar(256) NOT NULL,
col2 varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `test` ADD PRIMARY KEY (`id`);

ALTER TABLE `test` MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT;

INSERT INTO `test` (`col1`,`col2`) VALUES ('A', 'B');
INSERT INTO `test` (`col1`,`col2`) VALUES ('A', 'C');
INSERT INTO `test` (`col1`,`col2`) VALUES ('A', 'D');
INSERT INTO `test` (`col1`,`col2`) VALUES ('B', 'D');
INSERT INTO `test` (`col1`,`col2`) VALUES ('B', 'E');
INSERT INTO `test` (`col1`,`col2`) VALUES ('C', 'F');

EXPECTED RESULT:

A
B
C
D
E
F
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • The code looks like MySQL, so I removed the other database tags. – Gordon Linoff May 12 '21 at 16:49
  • Can’t help you with your homework but look into MySQL distinct keywork. Refer : https://stackoverflow.com/questions/7250566/mysql-select-distinct#:~:text=DISTINCT%20is%20not%20a%20function,the%20columns%20of%20another%20row. – Mayur May 12 '21 at 18:20

1 Answers1

0

Use union:

select col1
from test
union   -- on purpose to remove duplicates
select col2
from test;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786