选择:
select ac_subject,
max(case when type = '6' then contact end) phone,
max(case when type = 'E' then contact end) email
from kontakti
group by ac_subject
;
测试 DDL:
create table kontakti (
ac_subject varchar(8),
type char(1),
contact varchar(256)
);
INSERT INTO kontakti
(`ac_subject`, `type`, `contact`)
VALUES
('a1', 'E', 'email@gmai.com'),
('a1', '6', '41895478'),
('b2', 'E', 'aa@emai.si'),
('v5', '6', '243243'),
('v5', 'E', 'me@email.si')
;
create table kontakti_temp as
select ac_subject,
max(case when type = 'E' then contact end) email,
max(case when type = '6' then contact end) phone
from kontakti
group by ac_subject
;
输出:
ac_subject |
phone |
email |
a1 |
41895478 |
email@gmai.com |
b2 |
(null) |
aa@emai.si |
v5 |
243243 |
me@email.si |