选择除minimun以外的所有重复记录

kcugc4gi  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我在mysql中有一个表,其名称如下

我有两件事要处理
1-停用所有未使用的书籍

isActive = 1 - Active
isActive = 0  - Inactive

is_inuse = 1 - in use
is_inuse = 0 - not in use
I have the query as such

. . .

update books 
set is_active=0 
where book_name in (select (book_name) 
                    from books  
                    group by book_name 
                    having count(1) >1
                   ) and 
      is_inuse != 1;

2-重命名除最小图书id外的所有重复图书,在图书名称前附加图书id。很难对这个案子进行调查。

rhfm7lfc

rhfm7lfc1#

您应该能够使用下面的sql获取重复图书名称的列表

select t1.book_id, t1.book_name, concat(t1.book_id, t1.book_name) as new_name 
from table1 t1,
(
select book_name, min(book_id) as book_id from table1
group by book_name
) t2
where t1.book_name = t2.book_name
and t1.book_id != t2.book_id

相关问题