2

I would like to delete set of rows, which have same property like this SQL select.

SELECT item_id, count(*) as item_in_order
FROM items
GROUP BY order_id
HAVING item_in_order = 1

In words it means, that I have items of orders and I would like to delete rows, which are from orders with only one Item.

For example:

item_id    order_id
1          1
2          2
3          2
4          3
5          3
6          4

So I would like to delete rows with item_id 1 and 6.

8
  • nt clear.. tell little more.. you want to delete the record which u can select using this select query?? Commented Jan 29, 2014 at 14:32
  • Yes, exactly. I would like to delete set of rows form SELECT query that a post Commented Jan 29, 2014 at 14:35
  • 1
    This topic is silimar like stackoverflow.com/questions/4562787/… , but query from this delete me each record. And I have got around 300k rows so query execution time was very long thanks to subslecet in subselect Commented Jan 29, 2014 at 14:39
  • I fix it with new temporary table with syntax: INSERT items_temp SELECT * FROM items GROUP BY order_id HAVING count(*) > 1 Commented Jan 29, 2014 at 15:13
  • yes this is what I suggested u.. right.. !! Commented Jan 29, 2014 at 15:14

1 Answer 1

5

you can use this query

delete from items
where order_id in 
   (SELECT order_id
    FROM items
    GROUP BY order_id
    HAVING count(*)  = 1
   )

it will delete the row where order_id exist only once in items table

Sign up to request clarification or add additional context in comments.

3 Comments

Looks clear, I tried exactly this same but result is error: eror in query (1093): You can't specify target table 'items' for update in FROM clause
Yes I use. Is possible delete from same query that I select? I do not understant it. I think it is similar like this stackoverflow.com/questions/45494/…
hey dont mind but for time being u can try by duplicating this table and after deleting the record just delete the duplicate table.. does it make any sense??

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.