mysql - Removing duplicate SQL records to permit a unique key -


I have a table ('sale') in a MYSQL DB, which has a unique obligation to stop duplicates. needed. Prior to deleting deception and determining the barrier, it is proving to be a bit difficult.

  • product_id
  • table structure (simplified):

    • 'id (unique, autoinc)'

    To implement the specificity for the target product_id, the de-duping policy I want to apply is to remove all the most recently created duplicate records, such as: Supreme ID

    or any other To go the way, I would like to remove duplicate records, except that the IDs matched to the following query, excluding the existing Protecting Non-Corrupted Records:

      Select ID from internal joining of sales (calculation of product_id from max_id's sales group as product_id, max (id) & gt; 1) s.product_id and s.id = groupedByProdId.maxId  

    I have fought on this on two fronts - write the query to select the correct record to delete and then in MESQL Includes obstruction, where the FRRL segment from the subdivision of DELET does not refer to the same table Maybe the data from which were removed.

    I examined the answer and seemed to be dealing with this topic, but is specific to SQL Server, although I do not consider this question to be duplicated. In response to your comment, this is a query that works in MySQL:

    P>

      Remove yourTable from YourTable Join yourTable yt2 on Table.product_id = yt2.product_id and yourTable.id  

    This will only remove duplicate rows internal included will filter the latest line for each product, even if there is a second line for the same product May not exist.

    If you try to nick the table after FROM , MySQL requires you to specify the name of the database, such as:

      Delete & lt; DatabaseName & gt; YT.product_id = yt2.product_id and yt.id from yourTable yt & lt; But join your table yt2; yt2.id;  

    Comments