Insert Into in mysql 大小写时

Insert Into in mysql Case when

我有 2 个 table,我做了一个内部连接。

1)car_table

|car_id|  cars   |
|----- | -----   | 
|1     | Passat  |

2)property_model

|model_id|  p_value |
|--------|   -----  | 
|1       |   year   |
|2       |   color  |
|3       |   gear   |

3)property_value

|value_id| car_id| model_id| rs_value |
|--------|-----    | -----   | -----    |
|1       |   1     |   1     | 2020     |
|2       |   1     |   2     | Black    |
|3       |   1     |   3     | Manuel   |

我按照这样的方式做了,但是代码报错了

INSERT INTO property_value (model_id,rs_value)
SELECT model_id,car_id
    CASE model_id
        WHEN 1 AND car_id=1  THEN '2020' 
        WHEN 2 AND car_id=1  THEN 'Black'
        WHEN 3 AND car_id=1  THEN 'Manuel' 
      
    END
FROM property_model

如何php添加新数据从property_modeltable到property_valuetable?

使用 SELECT 查询作为要插入的数据源:

INSERT INTO property_value (model_id, rs_value)
SELECT model_id,
    CASE model_id
        WHEN 1 THEN '2020'
        WHEN 2 THEN 'Black'
        WHEN 3 THEN 'Automatic'
    END
FROM property_model

您还可以使用 ELT() 函数在参数列表中按索引 select 一个值。

INSERT INTO property_value (model_id, rs_value)
SELECT model_id, ELT(model_id, '2020', 'Black', 'Automatic')
FROM property_model

对于你的第二个查询,我想应该是这样的:

INSERT INTO property_value (model_id, car_id, rs_value)
SELECT model_id, 1,
    CASE model_id
        WHEN 1 THEN '2020' 
        WHEN 2 THEN 'Black'
        WHEN 3 THEN 'Manuel' 
      
    END
FROM property_model