Mysql 选择要删除的重复行?
我使用它来删除MySql/MariaDB表中的重复记录,因为三列是相同的Mysql 选择要删除的重复行?,mysql,sql,mariadb,Mysql,Sql,Mariadb,我使用它来删除MySql/MariaDB表中的重复记录,因为三列是相同的 DELETE a FROM stest as a, stest as b WHERE (a.facility_id=b.facility_id OR a.facility_id IS NULL AND b.facility_id IS NULL) AND (a.inspection_date=b.inspection_date OR a.inspection_date IS NULL AND b.inspe
DELETE a FROM stest as a, stest as b
WHERE
(a.facility_id=b.facility_id OR a.facility_id IS NULL AND b.facility_id IS NULL)
AND (a.inspection_date=b.inspection_date OR a.inspection_date IS NULL AND b.inspection_date IS NULL)
AND (a.deficiency_tag=b.deficiency_tag OR a.deficiency_tag IS NULL AND b.deficiency_tag IS NULL)
AND a.recno < b.recno;
我想做的是,在有重复记录的地方,保留长度最大的一个inspection_text列。很可能,检查文本列是相同的,但如果不是,我想删除较小的列
有人能告诉我如何修改上述语句以添加此条件吗
我也很好奇删除是如何工作的,但是如果我将DELETE a更改为SELECT a.*它不会显示要删除的行,而是显示表中的所有行?要获取要删除的值,可以使用并与检查文本的max_len值进行内部联接 对于双重许可行,删除长度为max_len的行
delete from stest
inner join (
select facility_id, deficiency_tag, inspection_date , max(length( inspection_text)) as max_len from stest
where ( facility_id, deficiency_tag, inspection_date ) in (
select facility_id, deficiency_tag, inspection_date
from stest
group by facility_id, deficiency_tag, inspection_date
having count(*) > 1
)
group by facility_id, deficiency_tag, inspection_date
) t on stest.facility_id = t.facility_id
and stest.deficiency_tag = t.deficiency_tag
and stest.inspection_date = t.inspection_date
and length( stest.inspection_text) <> t.max_len
没有内部联接的同一版本,但其中
delete from stest , (
select
facility_id
, deficiency_tag
, inspection_date
, max( length( inspection_text) ) as my_max_len
from stest, (
select
facility_id
, deficiency_tag
, inspection_date
from stest
group by facility_id, deficiency_tag, inspection_date
having count(*) > 1
) t2 where stest.facility_id = t2.facility_id and stest.deficiency_tag = t2.deficiency_tag and stest.inspection_date = t2.inspection_date
group by facility_id, deficiency_tag, inspection_date
) t where stest.facility_id = t.facility_id
and stest.deficiency_tag = t.deficiency_tag
and stest.inspection_date = t.inspection_date
and length( stest.inspection_text) <> t.my_max_len
如果我将DELETE a更改为SELECT a.*则不会显示要删除的行,而是显示表中的所有行?-您的查询没有执行您认为它正在执行的操作。优先于或。您需要使用另一组括号:ex-其中a.facility_id=b.facility_id或a.facility_id为NULL,b.facility_id为NULL。所有其他WHERE子句都需要相同的更新。您在SELECT语句中看到的结果是100%正确的。此外,请停止使用隐式连接语法。这在25年前被ANSI-92取代。为每个组分配一个用户变量,该变量表示您的工厂、检验、缺陷、长度描述订购的每个组的行号。然后删除行号>1的位置,我在上述语句中得到一个错误,减去我更正的一个语法错误。。1064-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解第2行“内部连接选择设备id、缺陷标记、检查日期、最大值”附近使用的正确语法。我更正了表名。。还是会犯那个错误。我能够在内部联接中执行该语句,并且它可以工作。我看不出错误在哪里。。有什么想法吗?我正在使用5.5.52-MariaDB,如果这有区别的话。。。。也许这个版本不支持内部连接?只是在stest.inspection\u text.和答案之间留一个空格。为mariadb 5.5.52删除的空间我不知道发布日期2016-09-13
delete from stest , (
select
facility_id
, deficiency_tag
, inspection_date
, max( length( inspection_text) ) as my_max_len
from stest, (
select
facility_id
, deficiency_tag
, inspection_date
from stest
group by facility_id, deficiency_tag, inspection_date
having count(*) > 1
) t2 where stest.facility_id = t2.facility_id and stest.deficiency_tag = t2.deficiency_tag and stest.inspection_date = t2.inspection_date
group by facility_id, deficiency_tag, inspection_date
) t where stest.facility_id = t.facility_id
and stest.deficiency_tag = t.deficiency_tag
and stest.inspection_date = t.inspection_date
and length( stest.inspection_text) <> t.my_max_len