Jump to content

Can't delete duplicate rows in a table


stevepatd
Go to solution Solved by Barand,

Recommended Posts

I am trying to delete some rows that are duplicate, or almost duplicates, from a table.   I've spent too many hours trying different approaches and researching this and have to ask for help. 

From the test "print" statement I put in, it never gets inside the if statement, I never see "I am here" on the screen.  Not that it matters but I'm using php. 

From the attached picture of my database table, I want to delete the lines, with red arrows, that have duplicate dates/times that have the word 'hide' for the Code.  If both similar lines have 'hide' for the code then I only want one, remove the other (thus the Limit 1).   Any help is appreciated.  Thank you.

$remove = "DELETE FROM teacher_schedule
           WHERE (Code='hide')
           ORDER BY DateTime
           LIMIT 1";
if (@mysqli_query ($connection,$remove)) {
  print "I am here";
  }

 

TableSample.png

Edited by stevepatd
add clarification
Link to comment
Share on other sites

  • Solution

As all records in a relational DB should have a primary key I'll assume yours do.

BEFORE

+---------------------+------+----------------+
|                   1 | hide | 20211123073000 |
|                   2 | hide | 20211123073000 |
|                   3 | hide | 20211123074500 |
|                   4 | hide | 20211123074500 |
|                   5 |      | 20211123080000 |
|                   6 | hide | 20211123080000 |
|                   7 |      | 20211123081500 |
|                   8 | hide | 20211123081500 |
|                   9 |      | 20211123083000 |
|                  10 | hide | 20211123083000 |
+---------------------+------+----------------+

QUERY

DELETE teacher_schedule
FROM teacher_schedule
     JOIN
     (
		SELECT code
			 , sched_time
			 , MAX(teacher_schedule_id) as teacher_schedule_id
		FROM teacher_schedule
		WHERE code = 'hide'
		GROUP BY code, sched_time
		HAVING count(*) > 1
	  ) dupes USING (teacher_schedule_id);

AFTER

+---------------------+------+----------------+
| teacher_schedule_id | code | sched_time     |
+---------------------+------+----------------+
|                   1 | hide | 20211123073000 |
|                   3 | hide | 20211123074500 |
|                   5 |      | 20211123080000 |
|                   6 | hide | 20211123080000 |
|                   7 |      | 20211123081500 |
|                   8 | hide | 20211123081500 |
|                   9 |      | 20211123083000 |
|                  10 | hide | 20211123083000 |
+---------------------+------+----------------+

 

Link to comment
Share on other sites

I allow duplicates since the administrator can double book, I want to do the cleanup in the backend.  I gave some thought to having an integer  index like you show but thought I could get away without it.  I will add that.  The table will eventually have data from several schools so I do have a column for SchoolID.  Will your approach allow me to keep the data from other school buildings intact?  Thanks for the help, I will give this a try.

 

Link to comment
Share on other sites

24 minutes ago, stevepatd said:

the administrator can double book

Is that because they want or need to, or because there is nothing to stop them them doing it accidentally?

You could add a UNIQUE constrint on (code, datetime) columns to prevent accidental duplicates.

BTW, as you will be testing deletions (not undoable) I recommend you back up before testing on any live data.

Edited by Barand
Link to comment
Share on other sites

This is for setting up parent teacher conferences.  Sometimes they will set up one 'day' as the morning, then the second 'day' as the afternoon of the same day as that morning session.  I want to merge the two getting rid of the hidden timeslots and keeping the ones that are open to schedule.   I wish they wouldn't do this but instead of giving them an error message I would rather handle it.

Thanks for the help, it seems to be working now.

Edited by stevepatd
spelling mistake
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.