SQL Delete rows based on query from same table?

Tom Staels

Given this query :

SELECT cur.`id`
FROM `current` AS cur
LEFT OUTER JOIN (
    SELECT MAX(EndDay_id.id) as id, EndDay_id.server_id
    FROM current as EndDay_id
    GROUP BY server_id, gameday
    ) AS EndDay
ON cur.id = EndDay.id
WHERE EndDay.id IS null

How do I delete the rows from table 'current' whose id field is in the resultset of previous query ?

Svein Fidjestøl

Try this

DELETE cur FROM `current` AS cur
LEFT OUTER JOIN (
    SELECT MAX(EndDay_id.id) as id, EndDay_id.server_id
    FROM current as EndDay_id
    GROUP BY server_id, gameday
    ) AS EndDay
ON cur.id = EndDay.id
WHERE EndDay.id IS null

I tested it and this should work: http://sqlfiddle.com/#!2/a47c81

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL Delete rows based on query from same table?

From Dev

BigQuery SQL delete rows from a target table based on a condition

From Dev

BigQuery SQL delete rows from a target table based on a condition

From Dev

Oracle SQL query a table and delete from an other based on the results

From Dev

Count rows from another SQL table in the same query

From Dev

How to delete the same table fields with an sql query

From Dev

Cannot delete rows from a table SQL

From Dev

How to Use Delete Triggers to Delete Rows From Same Table?

From Dev

delete rows based in the value of the previous row in the same table

From Dev

Delete Rows in SQL Table Based on Difference Between Values in Another Table

From Dev

sql query to delete parent table rows which are not used in child table

From Dev

sql query to delete parent table rows which are not used in child table

From Dev

SQL Server : update rows based other rows in the same table

From Dev

SQL Query to return the sum of balances from 1 or more rows from the same table

From Dev

How to delete rows from a table based on a join with another table?

From Dev

SQL query to extract Dates from table based on the value of other column in the same table?

From Dev

SQL - Delete row based on number of rows in another table

From Dev

SQL query: same rows

From Dev

SQL query: same rows

From Dev

Delete rows from table

From Dev

SQL Query - Return rows where based on dual conditioning of same column

From Dev

How to count rows from a different table based on same date range

From Dev

delete records which are selected from select query on the same table

From Dev

delete records which are selected from select query on the same table

From Dev

SQL, How to delete rows from related tables using a query?

From Dev

Update SQL query to change a field based on ID in same table

From Dev

How to delete unknown number of rows from sql table

From Dev

Delete limited n rows from oracle sql table

From Dev

SQL - Delete table with 372 million rows starting from first row

Related Related

  1. 1

    SQL Delete rows based on query from same table?

  2. 2

    BigQuery SQL delete rows from a target table based on a condition

  3. 3

    BigQuery SQL delete rows from a target table based on a condition

  4. 4

    Oracle SQL query a table and delete from an other based on the results

  5. 5

    Count rows from another SQL table in the same query

  6. 6

    How to delete the same table fields with an sql query

  7. 7

    Cannot delete rows from a table SQL

  8. 8

    How to Use Delete Triggers to Delete Rows From Same Table?

  9. 9

    delete rows based in the value of the previous row in the same table

  10. 10

    Delete Rows in SQL Table Based on Difference Between Values in Another Table

  11. 11

    sql query to delete parent table rows which are not used in child table

  12. 12

    sql query to delete parent table rows which are not used in child table

  13. 13

    SQL Server : update rows based other rows in the same table

  14. 14

    SQL Query to return the sum of balances from 1 or more rows from the same table

  15. 15

    How to delete rows from a table based on a join with another table?

  16. 16

    SQL query to extract Dates from table based on the value of other column in the same table?

  17. 17

    SQL - Delete row based on number of rows in another table

  18. 18

    SQL query: same rows

  19. 19

    SQL query: same rows

  20. 20

    Delete rows from table

  21. 21

    SQL Query - Return rows where based on dual conditioning of same column

  22. 22

    How to count rows from a different table based on same date range

  23. 23

    delete records which are selected from select query on the same table

  24. 24

    delete records which are selected from select query on the same table

  25. 25

    SQL, How to delete rows from related tables using a query?

  26. 26

    Update SQL query to change a field based on ID in same table

  27. 27

    How to delete unknown number of rows from sql table

  28. 28

    Delete limited n rows from oracle sql table

  29. 29

    SQL - Delete table with 372 million rows starting from first row

HotTag

Archive