Jump to content

Recommended Posts

How do I write a query that deletes the record and never deletes the maximum value of a certain unique field?

 

Example:

 

Shall we say I have 50,000 records of my table composed of fields such as first_name, last_name and scores, and I have 100 unique value of a 'last_name' field and each has a maximum value of column 'score' field. And I want to delete the repetitive 'last_name field' but not deleting the record that has the maximum value of column so that at least one record of that particular person remain in my table and that is the maximum value. How will I do that?

 

 

Thanks in advance.

i pressumed ur table structure is sum wat lyk ds....

ScoreKey      LastName  Score

  1                Reyes        5

  2                Cruz          10

  3                Reyes        3

  4                Reyes        6

  5                Cruz          2

 

try dis....

 

$score = mysql_query("select LastName, ScoreKey, max(Score)

                                from score_table

                                group by Score");

while( $max_score = mysql_fetch_array($score)) {

      $LastName = $max_score['LastName'];

      $ScoreKey  = $max_score['ScoreKey'];

      mysql_query("delete from score_table

                          where LastName = '$LastName'

                          and ScoreKey != $ScoreKey ");

}

 

not sure wd the syntax...but hop ul get the idea from this....

godblez...

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.