반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[MySql] Union Query 본문

Database

[MySql] Union Query

닉의네임 2022. 3. 15. 20:30
반응형

Sample

CREATE TABLE `union_test_a` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
    `item_number`  varchar(25)        NOT NULL                COMMENT '상품번호',
    `ranking`      varchar(2)         NOT NULL DEFAULT '1'    COMMENT '우선순위',
    PRIMARY KEY (`id`),
    KEY `idx_sch_edlp` (`item_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='TEST_A';


CREATE TABLE `union_test_b` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',
    `item_no`   varchar(25)        NOT NULL                COMMENT '상품번호',
    `ranking`   varchar(2)         NOT NULL DEFAULT '1'    COMMENT '우선순위',
    PRIMARY KEY (`id`),
    KEY `idx_sch_edlp` (`item_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='TEST_B';
INSERT INTO `doo`.`union_test_a` (`item_number`, `ranking`) VALUES ('292939882','1');
INSERT INTO `doo`.`union_test_a` (`item_number`, `ranking`) VALUES ('123334234','2');
INSERT INTO `doo`.`union_test_a` (`item_number`, `ranking`) VALUES ('675866456','3');
INSERT INTO `doo`.`union_test_b` (`item_no`, `ranking`) VALUES ('986756745','4');
INSERT INTO `doo`.`union_test_b` (`item_no`, `ranking`) VALUES ('196456435','5');
INSERT INTO `doo`.`union_test_b` (`item_no`, `ranking`) VALUES ('454345235','6');
INSERT INTO `doo`.`union_test_b` (`item_no`, `ranking`) VALUES ('856456456','7');
SELECT item_number FROM doo.union_test_a
 union 
SELECT item_no as item_number FROM doo.union_test_b;

-- 중복허용
SELECT item_number FROM doo.union_test_a
 union all
SELECT item_no as item_number FROM doo.union_test_b;
반응형

'Database' 카테고리의 다른 글

[mysql] left join 안에서 group_concat  (0) 2023.03.14
[mysql] PRIMARY KEY, UNIQUE KEY  (0) 2022.04.23
[Mysql] View  (0) 2022.04.01
[Mysql] Procedure  (0) 2022.04.01
[MySql] Event Scheduler  (0) 2022.04.01
Comments