我不知道如何將一列分成兩列以不同的條件分開。
如果聯系人型別 == 'E' 想保存 AS 電子郵件,否則型別 == '6' 保存 AS 電話。
請幫忙!

uj5u.com熱心網友回復:
你想要 CASE 運算式
select *,
case when type = 'E' then contact end email,
case when type = '6' then contact end phone
from KONTAKTI
uj5u.com熱心網友回復:
如果您想將這些列添加到表中,有兩個選項可以實作此目的。
首先創建具有所需更改的 kontakti 副本。請注意,我必須假設您的資料型別。
create table kontakti_temp (
ac_subject varchar(8),
type char(1),
phone varchar(16),
email varchar(256)
);
然后將來自 kontakti 的資料插入到 kontakti_temp 中,并使用大小寫拆分聯系人欄位。
insert into kontakti_temp
select ac_subject
, type
, case when type = '6' then contact end as phone
, case when type = 'E' then contact end as mail
from kontakti;
然后相應地重命名兩個表
或者您可以使用創建表作為陳述句
create table kontakti_temp as
select ac_subject
, type
, case when type = '6' then contact end as phone
, case when type = 'E' then contact end as mail
from kontakti;
kontakti_temp 中列的資料型別將派生自上述 select 陳述句回傳的資料型別。
uj5u.com熱心網友回復:
選擇:
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
;
測驗 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 protected]'),
('a1', '6', '41895478'),
('b2', 'E', '[email protected]'),
('v5', '6', '243243'),
('v5', 'E', '[email protected]')
;
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 |
email |
phone |
|---|---|---|
a1 |
[email protected] |
41895478 |
b2 |
[email protected] |
(null) |
v5 |
[email protected] |
243243 |
uj5u.com熱心網友回復:
這不好。我需要這樣的結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/417215.html
標籤:
上一篇:查找Maxmysql的Max
