I have 2 tables and I made an inner join.
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 |
I followed a way like this but the code gives an error
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
How can php I add a new data from property_model table to property_value table?
Use a SELECT
query as the source of data to insert:
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
You can also use the ELT()
function to select a value by index in the argument list.
INSERT INTO property_value (model_id, rs_value)
SELECT model_id, ELT(model_id, '2020', 'Black', 'Automatic')
FROM property_model
For your second query, I guess it should be like this:
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