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

Delete limited n rows from oracle sql table

From Dev

Delete rows from table

From Dev

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

From Dev

SQL query: same rows

From Dev

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

From Dev

How to delete unknown number of rows from sql table

From Dev

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

From Dev

SQL Delete rows based on query from same table?

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

How to delete the same table fields with an sql query

From Dev

Count rows from another SQL table in the same query

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Cannot delete rows from a table SQL

From Dev

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

From Dev

SQL query: same rows

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

Related Related

  1. 1

    Delete limited n rows from oracle sql table

  2. 2

    Delete rows from table

  3. 3

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

  4. 4

    SQL query: same rows

  5. 5

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

  6. 6

    How to delete unknown number of rows from sql table

  7. 7

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

  8. 8

    SQL Delete rows based on query from same table?

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

    How to delete the same table fields with an sql query

  14. 14

    Count rows from another SQL table in the same query

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

    Cannot delete rows from a table SQL

  20. 20

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

  21. 21

    SQL query: same rows

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

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

HotTag

Archive