Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete only one row if several found?

Tags:

sql

postgresql

I have a table that looks like this.

CREATE TABLE tshirt
(
  id serial,
  sku character varying(255) NOT NULL
);

I want to delete only one row with my wanted sku, but DELETE FROM tshirt WHERE sku='%s'; deletes all entries with that sku. How can I do this ?

like image 350
Marijus Avatar asked Dec 22 '13 11:12

Marijus


People also ask

How do you delete a row if a cell contains?

Go ahead to right click selected cells and select the Delete from the right-clicking menu. And then check the Entire row option in the popping up Delete dialog box, and click the OK button. Now you will see all the cells containing the certain value are removed.


2 Answers

Not the best way but, you could do this:

DELETE FROM tshirt
WHERE id IN (
   SELECT id FROM
   tshirt WHERE sku='%s' LIMIT 1
)
like image 73
Troydm Avatar answered Dec 10 '22 03:12

Troydm


This SQL Server DELETE TOP example would delete the first record from the employees table where the last_name is 'Anderson'. If there are other records in the employees table that have a last_name of 'Anderson', they will not be deleted by the DELETE TOP statement.

DELETE TOP(1)
FROM employees
WHERE last_name = 'Anderson';
like image 34
Muhammad Hassan Avatar answered Dec 10 '22 02:12

Muhammad Hassan