mysql 去除重复数据 语句

纠结的过程:

mysql> select * from role group by role_name,deleted;
+---------+-----------+---------+
| role_id | role_name | deleted |
+---------+-----------+---------+
| 2       | xue       | 12      |
| 1       | zhao      | 12      |
| 3       | zhao      | 13      |
+---------+-----------+---------+
3 rows in set
mysql>  delete from role c where c.role_id not in (select b.role_id from role c group by role_name,deleted);
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘where c.role_id not in (select b.role_id from role c group by role_name,deleted)‘ at line 1

mysql>  select *  from role c where c.role_id not in (select b.role_id from role b
 group by role_name,deleted);
+---------+-----------+---------+
| role_id | role_name | deleted |
+---------+-----------+---------+
| 4       | xue       | 12      |
+---------+-----------+---------+
1 row in set

mysql>  delete from role c where c.role_id not in (select b.role_id from role b  group by role_name,deleted);
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘where c.role_id not in (select b.role_id from role b  group by role_name,deleted‘ at line 1

mysql>  delete from role  where role_id not in (select b.role_id from role b  group by role_name,deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause

mysql>  delete from role  where role_id not in (select role_id from role   group by role_name,deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause

mysql>  delete from role  where not role_id  in (select role_id from role   group by role_name,deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause
mysql>  delete from role  where role_id not in (select t.role_id from role t  group by role_name,deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause
mysql>  delete from role  where role_id not in (select t.role_id from role t  group by t.role_name,t.deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause
mysql>  delete from role  where role_id NOT IN (select t.role_id from role t  group by role_name,deleted);
1093 - You can‘t specify target table ‘role‘ for update in FROM clause
mysql>  delete from role  where role_id NOT IN select *  from ( (select t.role_id from role t  group by role_name,deleted));
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘select *  from ( (select t.role_id from role t  group by role_name,deleted))‘ at line 1
mysql>  delete from role  where role_id NOT IN ( select *  from ( (select t.role_id from role t  group by role_name,deleted)));
1248 - Every derived table must have its own alias

mysql>  delete from role  where role_id NOT IN ( select *  from  ( select t.role_id from role t  group by t.role_name,t.deleted));
1248 - Every derived table must have its own alias

终于通了:

mysql>  delete from role  where role_id NOT IN ( select *  from  ( select t.role_id from role t  group by t.role_name,t.deleted) t1);
Query OK, 1 row affected

参考:
mysql比较作呕的一个delete in操作
http://www.educity.cn/wenda/594988.html

mysql 去除重复数据 语句

时间: 2024-12-11 15:54:32

mysql 去除重复数据 语句的相关文章

mysql 去除重复数据

1. 问题描述 有时load或者insert操作导致 表数据有重复 2. 解决方案 通过临时表.主键id.倒腾去重 示例 2.1  create table student( name varchar(30) not null default '', age smallint(3) not null default 0, love varchar(50) not null default '' ) 插入一些数据......(包含重复) insert into student(name,age,l

mysql 删除重复数据的sql语句

CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title,name,content FROM get_review_url GROUP BY no,title,name,content HAVING COUNT(*) > 1) AND id NOT IN (SELECT MIN(id) FROM get_review_url GROUP BY no,tit

mysql 去除重复 Select中DISTINCT关键字的用法

在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值.其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,我只有用二重循环查询来解决,而 这样对于一个数据量非常大的站来说,无疑是会直接影响到效率的.所以我花了很多时间来研究这个问题,网上也查不到解决方案,期间把容容拉来帮忙,

按天去除重复数据,为0则取0,否则取最大的那个值

测试数据: mysql> select * from t2; +----+--------+---------------------+------------+ | id | userid | inputDate | infoStatus | +----+--------+---------------------+------------+ | 1 | 1 | 2014-07-11 00:00:00 | 20013 | | 2 | 1 | 2014-07-11 00:00:00 | 0 |

MySQL 处理重复数据:防止表中出现重复数据、统计、过滤、删除重复数据

MySQL 处理重复数据 有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据. 本章节我们将为大家介绍如何防止数据表出现重复数据及如何删除数据表中的重复数据. 防止表中出现重复数据 你可以在 MySQL 数据表中设置指定的字段为 PRIMARY KEY(主键) 或者 UNIQUE(唯一) 索引来保证数据的唯一性. 让我们尝试一个实例:下表中无索引及主键,所以该表允许出现多条重复记录. CREATE TABLE person_tbl

sql查询去除重复值语句

sql 单表/多表查询去除重复记录 单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 ************************************************************************************ 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select

List集合去除重复数据

[转自]Java中List集合去除重复数据的方法 1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j > i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); }

springboot JPA 一对多关联查询 ,动态复杂查询 去除重复数据 in语句使用

目的:根据图书的发布地区查询图书信息实现步骤:1 实体配置one: 图书信息 bookmany: 地区信息 bookarea实体映射,单向映射 book 中增加 area 的集合 并设置 @JoinColumn(name="bookid")@OneToMany bookarea中不需要设置关系 编写查询语句Repository 继承 JpaSpecificationExecutor 重写findAll 并实现 Specification接口的 public Predicate toPr

mysql 删除重复数据

如题:mysql 数据库删除重复数据 因为是mysql 所以其他数据哭的命令在mysql 中是不能使用的.不要想当然的使用sql 脚本 delete from table1 where field1 in (select field1 from table1 group by field1 having count(field1) > 1) and rowid not in (select min(rowid) from table1 group by field1 having count(f