简体   繁体   中英

Delete Inner Join in MYSQL

I have 3 tables and I made an inner join.

car_table

|car_id|  car  |    
|------| ------| 
|1     | Passat|

property_model

|model_id|  p_value |
|--------|   -----  | 
|1       |   year   |
|2       |   color  |
|3       |   gear   |
|4       |   fuel   |
|5       |   km     |

property_value

|value_id| car_id  | model_id | rs_value |
|--------| -----   | -----    |  -----   |
|1       |   1     |     1    |2020      |
|2       |   1     |     2    |Black     |
|3       |   1     |     3    |Automatic |
|4       |   1     |     4    |Diesel    |
|5       |   1     |     5    |10.000    |

I want to delete values in property_value table. But I couldn't, can you help me?

The code i wrote is here.

DELETE property_value
FROM property_value

INNER JOIN cars ON
property_value.car_id=cars.car_id

INNER JOIN property_model ON
property_model.model_id=property_value.model_id

WHERE value_id='1'



Since you just want to delete value,so there is no need to use GROUP BY

DELETE
FROM property_value

INNER JOIN cars ON
property_value.car_id=cars.car_id

INNER JOIN property_model ON
property_model.model_id=property_value.model_id
WHERE value_id='1'

BTW there are two syntax errors in your original sql:

  1. DELETE FROM table not delete from column
  2. when you using GROUP BY ,it's need to be after WHERE

Teasing apart comments, I suspect you're after something like this...

DROP TABLE IF EXISTS car_table;

CREATE TABLE car_table
(car_id INT AUTO_INCREMENT PRIMARY KEY
,car  VARCHAR(12) NOT NULL
);

INSERT INTO car_table VALUES
(1,'Passat');

DROP TABLE IF EXISTS property_model;

CREATE TABLE property_model
(model_id INT AUTO_INCREMENT PRIMARY KEY
,p_value VARCHAR(12) NOT NULL
);

INSERT INTO property_model VALUES
(1,'year'),
(2,'color'),
(3,'gear'),
(4,'fuel'),
(5,'km');

DROP TABLE IF EXISTS property_value;

CREATE TABLE property_value
(value_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,car_id INT NOT NULL
,model_id INT NOT NULL
,rs_value VARCHAR(20) NOT NULL
);

INSERT INTO property_value VALUES
(1,1,1,'2020'),
(2,1,2,'Black'),
(3,1,3,'Automatic'),
(4,1,4,'Diesel'),
(5,1,5,'10000');

...

DELETE v
  FROM property_model a 
  JOIN property_value v 
    ON v.model_id = a.model_id
  JOIN car_table e
    ON e.car_id = v.car_id
 WHERE e.car_id = 1;
 
SELECT * FROM property_value;
Empty set (0.00 sec)

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