[英]How to merge null values with not null values of several columns in mysql table?
I have the following example table: 我有以下示例表:
|| *Column 1* || *Column 2* || *Column 3* || *Column 4* ||
|| a || null || b || a ||
|| b || f || null || f ||
|| null || a || a || b ||
The result table has to be: 结果表必须为:
|| *Column 1* || *Column 2* ||
|| a || b ||
|| b || f ||
|| a || b ||
Thanks! 谢谢!
SELECT one,
CASE WHEN one != two THEN two
WHEN one != three THEN three
WHEN one != four THEN four END AS two
FROM ( SELECT COALESCE(col1, col2, col3, col4) AS one,
COALESCE(col2, col3, col4, col1) AS two,
COALESCE(col3, col4, col1, col2) AS three,
COALESCE(col4, col1, col2, col3) AS four
FROM four_columns) AS h
Can't think of a simplier solution considering what result you wish in the example. 考虑到您在示例中希望得到的结果,无法想到一个更简单的解决方案。
This obviously obsoletes cases where these 4 columns have more than 2 different letters, due to only 2 columns existing. 由于仅存在2列,这显然淘汰了这4列具有2个以上不同字母的情况。 But it won't show duplicates and will not show NULL
if there is at least 2 unique letters. 但是,如果有至少2个唯一字母,它将不会显示重复项,也不会显示NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.