删除 MYSQL 中的内部联接
Delete Inner Join in MYSQL
我有 3 个 table,我做了一个内部连接。
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 |
我想删除 property_value table 中的值。但是我做不到,你能帮帮我吗?
我写的代码在这里
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'
因为你只是想删除值,所以没有必要使用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'
顺便说一句,您的原文有两个语法错误 sql:
DELETE FROM table
不从列中删除
- 当你使用
GROUP BY
时,需要在WHERE
之后
戏弄评论,我怀疑你在找这样的东西......
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)
我有 3 个 table,我做了一个内部连接。
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 |
我想删除 property_value table 中的值。但是我做不到,你能帮帮我吗?
我写的代码在这里
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'
因为你只是想删除值,所以没有必要使用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'
顺便说一句,您的原文有两个语法错误 sql:
DELETE FROM table
不从列中删除- 当你使用
GROUP BY
时,需要在WHERE
之后
戏弄评论,我怀疑你在找这样的东西......
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)