0

I'm having a hard time I have a lot of the same table fielt when inputting example:

---------------------------------<br/>
| id_pr       | id_pro   | id_cat  |
<br/>---------------------------------<br/>
| 1001        | 002      | 103     |
<br/>---------------------------------<br/>
| 1002        | 003      | 104     |
<br/>---------------------------------<br/>
| 1003        | 004      | 103     |
<br/>---------------------------------<br/>

id_cat field has the same data which is 103 how do i delete one of the same 103 data fields and when it is deleted data in

--------------------
| 1003 | 004 | 103 |
--------------------

also deleted, how do I enter the sql query ?

can you help me

2 Answers 2

1

You should use exists as follows:

Delete from your_table t
 Where exists
       (Select 1 from your_table tt
         Where t.id_cat = tt.id_cat
           And t.id_par > tt.id_par);
Sign up to request clarification or add additional context in comments.

Comments

0

You can keep the smallest id using join with an aggregation query:

delete t
    from t join
         (select id_cat, min(id_pr) as min_id_pr
          from t
          group by id_cat
          having count(*) > 1
         ) tt
         on t.id_cat = tt.id_cat and t.id_pr > tt.min_id_pr

2 Comments

I tried to enter the message in mysql but an error message appeared #1146 - Table 'data_story' doesn't exist
@urlsnort . . . You need a table that does exist, I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.