简体   繁体   中英

Concatenate two columns conditionally

I have MySQL table named table_1 :

mysql> select * from table_1;
+----------+-----------+----------+-------+
| col_1    | col_2     | col_3    | col_4 |
+----------+-----------+----------+-------+
|      123 | ABCD      | TTTTT    |     3 |
|    46542 | ABCD/HIJ  | AAAAAAAA |     9 |
|       23 | ABCDEFGH  | CCC      |    45 |
|     5675 | ABCD/HIJ  | NNNN     |     0 |
|    65165 | NJFD      | QQQQ     |     0 |
|   165464 | NULL      | HHH      |     8 |
| 16546541 | ABCABC    | LLLLLL   |     0 |
|   764316 | ABCD/BBJH | NULL     |     3 |
|     3125 | NULL      | EEEEEE   |    91 |
+----------+-----------+----------+-------+
9 rows in set (0.00 sec)

DDL:

CREATE TABLE IF NOT EXISTS table_1 (
  col_1 INT(11) NOT NULL,
  col_2 VARCHAR(45) DEFAULT NULL,
  col_3 VARCHAR(10) DEFAULT NULL,
  col_4 INT(11) NOT NULL
);

Insert data:

INSERT INTO table_1(col_1,col_2,col_3,col_4)
VALUES (123,'ABCD','TTTTT',3),
       (46542,'ABCD/HIJ','AAAAAAAA',9),
       (23,'ABCDEFGH','CCC',45),       
       (5675,'ABCD/HIJ','NNNN',0),
       (65165,'NJFD','QQQQ',0),
       (165464,NULL,'HHH',8),
       (16546541,'ABCABC','LLLLLL',NULL),
       (764316,'ABCD/BBJH',NULL,3),
       (3125,NULL,'EEEEEE',91);

What I want to do is select col_1 and a new MySQL generated second column, say col_new.

  1. If col_2 is NULL then col_new should be NULL as well.
  2. If col_2 is NOT NULL but col_3 is NULL then col_new should be NULL as well.
  3. If both col_2 and col_3 are NOT NULL then col_new should be same as col_3.
  4. There is a special case in 3. If both col_2 and col_3 are NOT NULL and col_3 = 'CCC' then then col_new should be concat(col_3, 'v', col_4) which in this case should be 'CCCv45'.

I know I'll have to use concat like: select col_1, concat(col_3, 'v', col_4) from table_1

But how can I implement all the 4 conditions above?

Another way to write the CASE expression would be:

select t.*,
    case when col_2 is null then null
         when col_3='CCC' then concat(col_3, 'v', col_4)
         else col_3
    end as new_col2     
from table_1 t
SELECT col_1, 
CASE 
WHEN (col_2 IS NULL) OR (col_2 IS NOT NULL AND col_3 IS NULL) THEN NULL
WHEN col_2 IS NOT NULL AND col_3 = 'CCC' THEN concat(col_3, 'v', col_4)
WHEN col_2 IS NOT NULL AND col_3 IS NOT NULL THEN col_3
END
FROM table_1

http://sqlfiddle.com/#!9/eb2550/5

Soltution 2

SELECT 
    col_1, 
    IF(
        (col_2 IS NULL) 
        OR (
            col_2 IS NOT NULL 
            AND col_3 IS NULL
        ), 
        NULL, 
        (
            IF(
                col_2 IS NOT NULL 
                AND col_3 = 'CCC', 
                concat(col_3, 'v', col_4), 
                col_3
            )
        )
    ) 
FROM 
    table_1

http://sqlfiddle.com/#!9/eb2550/9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM