使用CASE EXPRESSION的行的列

时间:2016-12-15 09:38:59

标签: mysql sql

我在mysql数据库的表中有很多记录,所以我只以两条记录为例。这是:

SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF, 
       a.PRE_APPROVED_DATE, a.FIRST_APPROVED_DATE, a.SECOND_APPROVED_DATE 
FROM tb_master_repair_estimate a
WHERE a.REPAIR_ESTIMATE_ID IN (21,23)

我明白了:

+--------------------+---------+-------------------+---------------------+----------------------+
| REPAIR_ESTIMATE_ID | EIR_REF | PRE_APPROVED_DATE | FIRST_APPROVED_DATE | SECOND_APPROVED_DATE |
+--------------------+---------+-------------------+---------------------+----------------------+
|                 21 | B6790   | 2016-12-15        | 2016-12-16          | NULL                 |
|                 23 | J6791   | 2016-12-10        | 2016-12-11          | 2016-12-13           |
+--------------------+---------+-------------------+---------------------+----------------------+
2 rows in set

我的目标是,我想在列PRE_APPROVED_DATE,FIRST_APPROVED_DATE,SECOND_APPROVED_DATE索引(在我看来)。 它应该是这样的:

PRE-> A
FIRST -> B
SECOND -> C

所以,我创建一个这样的查询:

SET @pre = "A";
SET @first = "B";
SET @two = "C";

SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF,
    CASE 
        WHEN a.PRE_APPROVED_DATE IS NOT NULL THEN @pre
        WHEN a.FIRST_APPROVED_DATE IS NOT NULL THEN @first
        WHEN a.SECOND_APPROVED_DATE IS NOT NULL THEN @two
        ELSE 0
    END AS `LEVEL`  
    FROM tb_master_repair_estimate a

    WHERE a.REPAIR_ESTIMATE_ID IN (21,23)

但我这样:

+--------------------+---------+-------+
| REPAIR_ESTIMATE_ID | EIR_REF | LEVEL |
+--------------------+---------+-------+
|                 21 | B6790   | A     |
|                 23 | J6791   | A     |
+--------------------+---------+-------+
2 rows in set

我需要这样

+--------------------+---------+-------+
| REPAIR_ESTIMATE_ID | EIR_REF | LEVEL |
+--------------------+---------+-------+
|                 21 | B6790   | A     |
|                 21 | B6790   | B     |
|                 23 | J6791   | A     |
|                 23 | J6791   | B     |
|                 23 | J6791   | C     |
+--------------------+---------+-------+

请告知...... 任何帮助它如此欣赏。

2 个答案:

答案 0 :(得分:1)

不需要案例表达,请使用//Bird class class Bird { //Your data members //Your member functions public void draw(SpriteBatch batch) { batch.draw(some_bird_image, bird_x_position, bird_y_position); } } //Render method in main class public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); for(Bird bird : birds) { bird.draw(batch); } batch.end(); }

UNION ALL

如果你想要那些特定的SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF, @pre as level FROM tb_master_repair_estimate a WHERE a.PRE_APPROVED_DATE IS NOT NULL -- "A" UNION ALL SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF, @first as level FROM tb_master_repair_estimate a WHERE a.FIRST_APPROVED_DATE IS NOT NULL -- "B" UNION ALL SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF, @second as level FROM tb_master_repair_estimate a WHERE a.SECOND_APPROVED_DATE IS NOT NULL -- "C" ,那么添加过滤器。

答案 1 :(得分:0)

> 你可以尝试

SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF,
    CASE 
        WHEN a.PRE_APPROVED_DATE IS NOT NULL THEN 'A'     
        ELSE '0'
    END AS 'LEVEL'  
    FROM tb_master_repair_estimate a
union all

SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF,
    CASE 
    WHEN a.FIRST_APPROVED_DATE IS NOT NULL THEN 'B'
        ELSE '0'
    END AS 'LEVEL'  
    FROM tb_master_repair_estimate a
union all

SELECT a.REPAIR_ESTIMATE_ID, a.EIR_REF,
    CASE 
    WHEN a.SECOND_APPROVED_DATE IS NOT NULL THEN 'C'
    ELSE '0'
    END AS 'LEVEL'  
    FROM tb_master_repair_estimate a