数据库 添加 联合索引
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库 添加 联合索引相关的知识,希望对你有一定的参考价值。
加联合索引
ALTER TABLE net
ADD UNIQUE KEY(time, id,name);
删除联合索引
ALTER TABLE net DROP INDEX `time`;
查询重复值
SELECT
*
FROM
`net`
WHERE
( `time`, `id`,`name` ) IN ( SELECT `time`, `id`,`name` FROM `net` GROUP BY `time`, `id`,`name` HAVING COUNT( 1 ) > 1 ) AND `id` NOT IN (SELECT MIN(`id`) FROM (SELECT * FROM `net`) b GROUP BY `time`, `id`,`name` HAVING COUNT(1) > 1);
删除重复值,保留ID 小的
DELETE FROM `net`
WHERE (`time`, `id`,`name` ) IN (SELECT `time`, `id`,`name` FROM (SELECT * FROM `net`) a GROUP BY `time`, `id`,`name` HAVING COUNT(1) > 1)
AND `id` NOT IN (SELECT MIN(`id`) FROM (SELECT * FROM `net`) b GROUP BY `time`, `id`,`name` HAVING COUNT(1) > 1);
以上是关于数据库 添加 联合索引的主要内容,如果未能解决你的问题,请参考以下文章