如何在全外连接中合并select 2个同名参数
How to merge select 2 same name param in full outer join
我有 2 个 table 具有相同的 trial_id
字段名称。对于 full outer join
,我想从每个单独的 table 中得到 trial_id
。但我想将它合并为一个trial_id
。我该怎么办?
SELECT ht.trial_id as trial_id, cts.trial_id AS trial_id, ht.subject, cts.trial_contract_name
FROM
hubspot_ticket ht
FULL OUTER JOIN cs_trial_sheet cts
ON cts.trial_id = ht.trial_id
预期结果:
只有一个 trial_id
列,并且不能为空。如果 ht.trial_id
-> 得到 cts.trial_id
.
select COALESCE(ht.trial_id, cts.trial_id) 作为 trial_id
可以根据MySQL的Join Documentation使用COALESCE()
:
The single result column that replaces two common columns is defined using the coalesce operation. That is, for two t1.a and t2.a the resulting single join column a is defined as a = COALESCE(t1.a, t2.a), where:
COALESCE(x, y) = (CASE WHEN x IS NOT NULL THEN x ELSE y END)
If the join operation is any other join, the result columns of the join consist of the concatenation of all columns of the joined tables.
试试这个:
select ht.subject, cts.trial_contract_name,
COALESCE(ht.trial_id, cts.trial_id) AS trial_id
FROM
hubspot_ticket ht
FULL OUTER JOIN cs_trial_sheet cts
ON cts.trial_id = ht.trial_id
但是,我不认为MySQL有FULL OUTER JOIN
,我通常用LEFT JOIN
、UNION
、RIGHT JOIN
作为替代。
我有 2 个 table 具有相同的 trial_id
字段名称。对于 full outer join
,我想从每个单独的 table 中得到 trial_id
。但我想将它合并为一个trial_id
。我该怎么办?
SELECT ht.trial_id as trial_id, cts.trial_id AS trial_id, ht.subject, cts.trial_contract_name
FROM
hubspot_ticket ht
FULL OUTER JOIN cs_trial_sheet cts
ON cts.trial_id = ht.trial_id
预期结果:
只有一个 trial_id
列,并且不能为空。如果 ht.trial_id
-> 得到 cts.trial_id
.
select COALESCE(ht.trial_id, cts.trial_id) 作为 trial_id
可以根据MySQL的Join Documentation使用COALESCE()
:
The single result column that replaces two common columns is defined using the coalesce operation. That is, for two t1.a and t2.a the resulting single join column a is defined as a = COALESCE(t1.a, t2.a), where:
COALESCE(x, y) = (CASE WHEN x IS NOT NULL THEN x ELSE y END)
If the join operation is any other join, the result columns of the join consist of the concatenation of all columns of the joined tables.
试试这个:
select ht.subject, cts.trial_contract_name,
COALESCE(ht.trial_id, cts.trial_id) AS trial_id
FROM
hubspot_ticket ht
FULL OUTER JOIN cs_trial_sheet cts
ON cts.trial_id = ht.trial_id
但是,我不认为MySQL有FULL OUTER JOIN
,我通常用LEFT JOIN
、UNION
、RIGHT JOIN
作为替代。