mysql 如何删除selecttoself表中的对

jhiyze9q  于 2023-05-21  发布在  Mysql
关注(0)|答案(4)|浏览(72)

我加入了一个表与它的自我,我有重复的对,因为我突出显示在下面的图像如何删除它们?

select DISTINCT A.name as name1 , B.name as name2
from (select name , ratings.* from reviewers inner join ratings on reviewers.id = 
ratings.reviewer_id ) A ,
(select name , ratings.* from reviewers inner join ratings on reviewers.id = 
ratings.reviewer_id ) B
where A.reviewer_id <> B.reviewer_id 
and A.book_id = B.book_id
order by name1 , name2 ASC
名称1名称2
爱丽丝·刘易斯伊丽莎白·布莱克
克里斯·托马斯约翰·史密斯
克里斯·托马斯迈克·白色
伊丽莎白·布莱克爱丽丝·刘易斯
伊丽莎白·布莱克杰克·绿色
杰克·绿色伊丽莎白·布莱克
乔·马丁内斯迈克·安德森
约翰·史密斯克里斯·托马斯
迈克·安德森乔·马丁内斯
迈克·白色克里斯·托马斯

上面的表曾经是image

hzbexzde

hzbexzde1#

你可以做

select Name1, Name2
from ...
where Name1 < Name2;

参见this example

nkoocmlb

nkoocmlb2#

要删除重复项,您首先需要找到它们:

SELECT 
  LEAST(name1,name2) as L,
  GREATEST(name1,name2) as G
FROM names
GROUP BY LEAST(name1,name2), GREATEST(name1,name2) 
HAVING count(*)>1;

然后您可以删除它们:

WITH cte as (
  SELECT 
    LEAST(name1,name2) as L,
    GREATEST(name1,name2) as G
  FROM names
  GROUP BY LEAST(name1,name2), GREATEST(name1,name2) 
  HAVING count(*)>1
)
DELETE FROM names
WHERE (name2,name1) in (select * from cte);

参见:DBFIDDLE

yshpjwxd

yshpjwxd3#

这可以使用greatestleast来识别跨列的重复项:
此查询将获得重复项:

select greatest(name, name2), least(name, name2)
from mytable
group by greatest(name, name2), least(name, name2)
having count(1) > 1

结果:

first_name  second_name
Jack Green  Elizabeth Black

然后:

DELETE t.* FROM mytable t
INNER JOIN (
  select greatest(name, name2) as first_name, least(name, name2) as second_name
  from mytable
  group by greatest(name, name2), least(name, name2)
  having count(1) > 1
) as s ON (t.name = s.first_name and t.name2 = s.second_name)
          OR (t.name2 = s.first_name and t.name = s.second_name)

Demo here

cdmah0mi

cdmah0mi4#

我创建了DDL和DML语句来重现数据库,并编写了一个检索不重复对的查询。以下是“构建”代码,可能对其他人有所帮助:

CREATE TABLE books (
  id INT PRIMARY KEY,
  title VARCHAR(100)
);

CREATE TABLE reviewers (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE ratings (
  id INT PRIMARY KEY,
  reviewer_id INT,
  book_id INT,
  rating INT,
  FOREIGN KEY (reviewer_id) REFERENCES reviewers(id),
  FOREIGN KEY (book_id) REFERENCES books(id)
);

-- Inserting sample records
INSERT INTO reviewers (id, name)
VALUES
  (1, 'Alice Lewis'),
  (2, 'Elizabeth Black'),
  (3, 'Chris Thomas'),
  (4, 'John Smith'),
  (5, 'Mike White'),
  (6, 'Jack Green'),
  (7, 'Joe Martinez'),
  (8, 'Mike Anderson');

INSERT INTO books (id, title)
VALUES
  (1, 'The Gulag Archipelago'),
  (2, 'One Day in the Life of Ivan Denisovich'),
  (3, 'Cancer Ward');

-- Insertion of rating records
INSERT INTO ratings (id, reviewer_id, book_id, rating)
VALUES
  (1, 1, 1, 4),
  (2, 1, 2, 3),
  (3, 2, 1, 5),
  (4, 2, 2, 4),
  (5, 2, 3, 2),
  (6, 3, 1, 3),
  (7, 3, 3, 4),
  (8, 4, 1, 2),
  (9, 4, 3, 3),
  (10, 5, 2, 5),
  (11, 6, 1, 1),
  (12, 6, 2, 3),
  (13, 6, 3, 4),
  (14, 7, 1, 3),
  (15, 7, 2, 4),
  (16, 8, 3, 2);

下面是重构后的查询:

SELECT DISTINCT
  A.name AS name1,
  B.name AS name2
FROM
  (
    SELECT
      reviewers.id,
      reviewers.name,
      ratings.book_id
    FROM
      reviewers
      INNER JOIN ratings ON reviewers.id = ratings.reviewer_id
  ) A
  JOIN (
    SELECT
      reviewers.id,
      reviewers.name,
      ratings.book_id
    FROM
      reviewers
      INNER JOIN ratings ON reviewers.id = ratings.reviewer_id
  ) B ON A.book_id = B.book_id
     AND A.id <> B.id
     AND A.name < B.name
ORDER BY
  name1,
  name2 ASC;

使用别名A和B两次使用相同的子查询,组合reviewersratings表并检索每对书评人-图书评分的数据。
然后,主外部查询从子查询结果中选择不同的审阅者姓名对。我们在3个条件下在子查询A和B之间使用JOIN

  • A.book_id = B.book_id所以这两对评论家对同一本书进行了评级。
  • A.id <> B.id过滤掉具有相同ID的审阅者对,防止自匹配。
  • A.name < B.name,以确保这些对的顺序与下面的输出一致,从而消除重复。这样,对于给定的配对,仅考虑一个名称组合,例如“Elizabeth Black-Jack绿色”而不是“Jack Green-Elizabeth Black”。

这是您将从重构后的查询中获得的输出:

+-----------------+-----------------+
|      name1      |      name2      |
+-----------------+-----------------+
| Alice Lewis     | Chris Thomas    |
| Alice Lewis     | Elizabeth Black |
| Alice Lewis     | Jack Green      |
| Alice Lewis     | Joe Martinez    |
| Alice Lewis     | John Smith      |
| Alice Lewis     | Mike White      |
| Chris Thomas    | Elizabeth Black |
| Chris Thomas    | Jack Green      |
| Chris Thomas    | Joe Martinez    |
| Chris Thomas    | John Smith      |
| Chris Thomas    | Mike Anderson   |
| Elizabeth Black | Jack Green      |
| Elizabeth Black | Joe Martinez    |
| Elizabeth Black | John Smith      |
| Elizabeth Black | Mike Anderson   |
| Elizabeth Black | Mike White      |
| Jack Green      | Joe Martinez    |
| Jack Green      | John Smith      |
| Jack Green      | Mike Anderson   |
| Jack Green      | Mike White      |
| Joe Martinez    | John Smith      |
| Joe Martinez    | Mike White      |
| John Smith      | Mike Anderson   |
+-----------------+-----------------+

相关问题