我今年学习MySQL添加这个。我试图删除我从这个select语句中得到的结果:
select distinct *
from films a
inner join films b
on trim(upper(a.titel)) = trim(upper(b.titel))
where a.filmID > b.filmID
但它们有一些问题:我们使用的数据库已损坏且包含许多错误。因此,测试代码并不总是具有我们需要的结果。所以我们基本上都是盲目的,只是解决问题而不能正确地测试它们。
由于截止日期是明天,我们解决了除此之外的所有问题。我想通过这样做来解决这个问题:
delete from films where (*) in (
select distinct *
from films a
inner join films b
on trim(upper(a.titel)) = trim(upper(b.titel))
where a.filmID > b.filmID
);
现在我收到了这个错误:
我的同学认为这只是数据库中的一个错误。但我怀疑情况并非如此。
然后我尝试了这个:
delete from films where titel in (
select distinct a.titel
from films a
inner join films b
on trim(upper(a.titel)) = trim(upper(b.titel))
where a.filmID > b.filmID
);
出现此错误:
答案 0 :(得分:2)
请考虑以下事项:
DROP TABLE IF EXISTS films;
CREATE TABLE films
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,title VARCHAR(50) NOT NULL
);
INSERT INTO films (title) VALUES
('The Shining'),
('Jaws'),
('Casablanca'),
('Bladerunner'),
('The Shining'),
('Casablanca');
SELECT *
FROM films x
JOIN films y
ON y.title = x.title
WHERE y.id < x.id;
+----+-------------+----+-------------+
| id | title | id | title |
+----+-------------+----+-------------+
| 5 | The Shining | 1 | The Shining |
| 6 | Casablanca | 3 | Casablanca |
+----+-------------+----+-------------+
DELETE x FROM films x JOIN films y ON y.title = x.title WHERE y.id < x.id;
Query OK, 2 rows affected (0.01 sec)
SELECT * FROM films;
+----+-------------+
| id | title |
+----+-------------+
| 1 | The Shining |
| 2 | Jaws |
| 3 | Casablanca |
| 4 | Bladerunner |
+----+-------------+
答案 1 :(得分:0)
(代表问题作者发布解决方案)。
delete x
from films x
join films y
on trim(upper(y.titel)) = trim(upper(x.titel))
where x.filmID > y.filmID;