Está en la página 1de 7

21 11887 82AE

678910 11 12 13 TOCA 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 CONFERENCISTA

CONSULTA PARA VERIFICAR LOS DUPLICADOS

Credenciales 387

Registrados 431

Reiniciar id de una tabla


ALTER TABLE nombre_tabla AUTO_INCREMENT=1

Consulta para verificar duplicados

SELECT id, nombre, ape_pat, ape_mat, COUNT( * )

FROM inscripcion

GROUP BY nombre, ape_pat, ape_mat

HAVING COUNT( * ) >1

LIMIT 0 , 30

ESTO DE ABAJO TODAVIA NO SE PARA QE SIRVE JEJE

SELECT id, nombre, ape_pat, ape_mat col3=count(*)

INTO holdkey

FROM inscripcion

GROUP BY nombre, ape_pat, ape_mat

HAVING count(*) > 1

SELECT DISTINCT inscripcion.*

INTO holddups

FROM inscripcion, holdkey

WHERE inscripcion.col1 = holdkey.col1

AND inscripcion.col2 = holdkey.col2


SELECT id, count(*)

FROM holddups

GROUP BY id

DELETE inscripcion

FROM inscripcion, holdkey

WHERE inscripcion.id = holdkey.id

INSERT inscripcion SELECT * FROM holddups

SELECT a1.id, a1.nombre, a1.ape_pat, a1.ape_mat

FROM inscripcion a1

INNER JOIN inscripcion a2 ON a1.id > a2.id

AND a1.nombre = a2.nombre

AND a1.ape_pat = a2.ape_pat

AND a1.ape_mat = a2.ape_mat;

DELETE FROM inscripcion WHERE id IN (

SELECT a1.id

FROM inscripcion a1

INNER JOIN inscripcion a2 ON a1.id > a2.id

AND a1.nombre = a2.nombre

AND a1.ape_pat = a2.ape_pat

AND a1.ape_mat = a2.ape_mat

);
SELECT DISTINCT id, nombre, ape_pat, ape_mat, COUNT( * )

FROM inscripcion

GROUP BY nombre, ape_pat, ape_mat

HAVING COUNT( * ) >1

LIMIT 0 , 30

CONSULTA BUENA QUE ELIMINO LOS DUPLICADOS DE UN DETERMINADO EVENTO ACTIVO

MySQL tiene restricciones en referencia a la tabla que va a eliminar de.

Puede trabajar alrededor de eso con una tabla temporal, como:

create temporary table tmpTable (id int);

insert tmpTable

(id)

select id

from inscripcion ins

where evento =(SELECT id FROM evento where estado='ACTIVO') and exists

select *

from inscripcion ins2

where ins2.nombre = ins.nombre

and ins2.ape_pat = ins.ape_pat

and ins2.ape_mat = ins.ape_mat

and ins2.id > ins.id


);

delete

from inscripcion

where ID in (select id from tmpTable);

CONSULTA QUE ACTUALIZA TODAS LAS COLUMNAS A UN ESTADO INACTIVO

UPDATE inscripcion SET estado='INACTIVO'

DELETE inscripcion_gral FROM inscripcion_gral where evento=(SELECT id FROM evento where


estado='ACTIVO')

COMO QUITAR LAS FILAS DUPLICADAS EN SQL SERVER

Lo primero que tenemos que hacer es identificar las filas duplicadas.

SELECT id, count(*)

FROM t1

GROUP BY id

HAVING count(*)>1

Con esta sentencia tenemos todos los id y las veces que se repiten.

Si tenemos pocas filas duplicadas podemos eliminarlas manualmente con esta sentencia:
1

set rowcount 1

delete from t1

where id=1 and id=2

Antes de eliminar las filas deberiamos comprobar si la fila entera esta duplicada

pq puede que sean los id iguales pero diferentes atributos, en ese caso deberiamos

analizar las filas para ver que datos incluidos son los validos.

Si en la tabla hay muchos conjuntos distintos de valores duplicados,

eliminarlos manualmente puede ser un trabajo duro. Podemos utilizar este procedimiento:

1 – Primero, ejecute la consulta anterior para saber cuantos conjuntos diferente tenemos:

SELECT id, count(*)

FROM t1

GROUP BY id

HAVING count(*) > 1

2 – Seleccione los valores de las claves duplicadas e inclúyalas en otra tabla.


1

SELECT id, col3=count(*)

INTO holdkey

FROM t1

GROUP BY id

HAVING count(*) > 1

3 – Seleccione las filas duplicadas e inclúyalas en una tabla contenedora, eliminando los
duplicados en el mismo proceso. Por ejemplo:

SELECT DISTINCT t1.*

INTO holddups

FROM t1, holdkey

WHERE t1.col1 = holdkey.col1

AND t1.col2 = holdkey.col2

4- En este momento, en la tabla holddups tendremos todas las filas unicas.

SELECT id, count(*)

FROM holddups
GROUP BY id

5- Elimine las filas duplicadas en la tabla original.

DELETE t1

FROM t1, holdkey

WHERE t1.id = holdkey.id

6 – Incluimos las filas unicas en la tabla original.

INSERT t1 SELECT * FROM holdups

PARA EVITAR TYPEAR MAS SE SELECCIONA TODOS LOS INSCRITOS DEL ANTERIOR EVENTO
UN AÑO ANTS Y SE LO VUELVE A CARGAR EN LA TABLA INSCRIPCION

CONSULTA PARA SELECCIONAR INSCRITOS DE UN DETERMINADO EVENTO

SELECT * FROM inscripcion where evento=(SELECT id FROM evento where estado='ACTIVO')

CONSULTA PARA ELIMINAR INSGRITOS DE LA TABLA GENERAL DE INSCRITOS

DELETE inscripcion_gral FROM inscripcion_gral where evento=(SELECT id FROM evento where


estado='ACTIVO')

También podría gustarte