sql-delete

How to remove mySQL duplicate entries

孤街醉人 提交于 2019-12-11 03:58:28
问题 I have a mySQL table with duplicate entries (perhaps in some cases multiple duplicates). I have column called id, which may contain duplicate ids and one called unique id which, as the name suggest contains unique ids. Using the following SQL statement I am able to select the duplicate row SELECT id, COUNT(id) AS NumOccurrences FROM `TABLE 3` GROUP BY id HAVING ( COUNT(id) > 1 ) but how do I delete (all but one of) them? 回答1: DELETE t1 FROM test t1, test t2 WHERE t1.unique_id > t2.unique_id

DELETE statement issues within a trigger definition

自作多情 提交于 2019-12-11 03:32:46
问题 I have created an insert/update trigger that is designed to update information in a different table based on the data being inserted. The last thing the trigger does (or is supposed to do) is remove all data from a target table with conditions that may have changed during the insert portions of the trigger. Everything appears to be working with the trigger except the final DELETE statement. It is executing the DELETE statement, but not following any of the conditions in the where clause. It

Delete duplicates in postgres

不想你离开。 提交于 2019-12-11 03:25:57
问题 I want to delete all but one row for a given duplicate "external_id". The query below takes about two minutes to run for my table of 5,000,000 rows, and I feel like there's got to be a quicker way of performing this task. "id" is the primary key, and "external_id" is a btree indexed column: delete from posts p1 using (select distinct on (1) external_id, id from posts order by 1 desc, 2 desc) p_recent where p1.external_id = p_recent.external_id and p1.id != p_recent.id; How can I improve the

SQL for delete query

孤街醉人 提交于 2019-12-11 03:14:10
问题 I'm trying to write an SQL query for my program, but I just can't figure out how. I don't know enough SQL. I'm trying to implement an online team system (for some website). I have two tables: teams | teamId, eventId teammembers | teamId, userId, status Now, I need to: delete all records in teammembers where the eventId for the corresponding teamId is 1. I'm trying: delete from teammembers where teamId=teams.teamId and teams.eventId=1; I'm not sure if this is really doing what I'm trying to do

SQL DELETE performance

孤者浪人 提交于 2019-12-11 03:13:41
问题 delete from a A where a.ID = 132. The table A contains around 5000 records and A.ID is the primary key in the table A. But it is taking a long time to delete . Sometimes its getting timed out also . That table contains three indexes and it is referenced by three foreign keys . Can anyone explain me why its taking long time even though we are deleting based on the primary key . And please tell me some way to optimize this problem ...? 回答1: Obviously it should not be taking a long time. However

Row is not being deleted because of cascade trigger updates this row

蓝咒 提交于 2019-12-11 02:27:06
问题 I have problem with deleteing rows in table which has trigger which invokes trigger of second table, which updates row in first table. Here is the description: Table A (id,b_table_count) Table B (id,a_table_id_fk) Table A has trigger BEFORE DELETE which has instructions: BEGIN DELETE FROM b where a_table_fk = OLD.id; RETURN OLD; END; Table B has trigger AFTER DELETE with instruction: UPDATE a SET b_table_count = b_table_count-1 WHERE OLD.a_table_id_fk = a.id; When I delete row from table A,

Deleting Rows: No Single Member Has More Than x Records

廉价感情. 提交于 2019-12-11 02:14:25
问题 I have a table structured like this: CREAT TABLE `member_logins` ( `id` bigint(10) unsigned not null auto_increment, `member_id` mediumint(8) unsigned not null, `date_created` datetime not null, PRIMARY KEY(`id`) ) ENGINE=InnoDB; I only want to keep 50 logins recorded for each member. So I'm looking for a way to DELETE rows on a per member basis, any rows past the most recent 50. Edit: I should have mentioned... This would be a nightly cron job. Not something that needs to be done in real

EclipseLink+SpringDataJPA: Single Table MultiTenancy-TENANT_ID is not added to WHERE clause for DELETE and UPDATE

﹥>﹥吖頭↗ 提交于 2019-12-11 01:17:14
问题 I am using EclipseLink And Spring Data JPA for Single Table Multi-Tenancy.(DISCRIMINATOR based model). I have customized Spring Data JPA SimpleJPAReposity to add below mentioned and required property on EntityManager . em.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT,"TENANT_1"); Every Spring Data JPA operations are now handled by CustomizedSimpleJpaRepository . I have configured it successfully and below all Spring DATA JPA operations are working fine, they are

Find duplicates or more in Mysql, delete them except the first one input

感情迁移 提交于 2019-12-11 00:56:57
问题 I have a table with rows like id, length, time and some of them are duplicates, where length and time is the same in some rows. I want to delete all copies of the first row submitted. id | length | time 01 | 255232 | 1242 02 | 255232 | 1242 <- Delete that one I have this to show all duplicates in table. SELECT idgarmin_track, length , time FROM `80dage_garmin_track` WHERE length in ( SELECT length FROM `80dage_garmin_track` GROUP BY length HAVING count(*) > 1 ) ORDER BY idgarmin_track, length

Passing table names in an array

杀马特。学长 韩版系。学妹 提交于 2019-12-10 23:17:24
问题 I need to do the same deletion or purge operation (based on several conditions) on a set of tables. For that I am trying to pass the table names in an array to a function. I am not sure if I am doing it right. Or is there a better way? I am pasting just a sample example this is not the real function I have written but the basic is same as below: CREATE OR REPLACE FUNCTION test (tablename text[]) RETURNS int AS $func$ BEGIN execute 'delete * from '||tablename; RETURN 1; END $func$ LANGUAGE