Error Code 1093 in DELETE statement - mysql

My query is:-
delete from api_data
WHERE local_id NOT IN( SELECT MAX(local_id)
FROM api_data
GROUP BY local_id);
But i am getting error which says:
You can't specify target table 'api_data' for update in FROM clause.
Any Help?

In MySQL you can't delete from the same table you are selecting from. But you can use another subquery to cover that
delete from api_data
WHERE local_id NOT IN
(
select * from
(
SELECT MAX(local_id) FROM api_data GROUP BY local_id
) tmp
);

Related

How to delete duplicate rows in MySQL table?

I am using the following query, which I saw from another stackoverflow question but I am getting error.
delete from mytable
where myid not in (
select max(myid)
from mytable
group by myid2)
Error:
#1093 - Table 'mytable' is specified twice, both as a target for 'DELETE' and as a separate source for data
Edit 2:
I also tried this query:
delete from mytable
where myid in (
SELECT
myid, COUNT(*)
FROM
mytable
GROUP BY
myid2
HAVING
COUNT(*) > 1)
And got this error:
#1241 - Operand should contain 1 column(s)
In MySQL, you need to use a JOIN for this purpose. I assume you mean something like this:
delete t
from mytable t left join
(select max(myid) as myid
from mytable
group by myid2
) tt
on t.myid = tt.myid
where tt.myid is null;
Where ? is whatever you really want to group by. Your version will not delete anything because the group by and max() use the same column.

How to delete already Select table?

I have selected Duplicate table using Following Query
SELECT
y.Entity,y.ExpenseTypeCode,y.ExpenseType,y.LOB,y.Center,y.Location,y.Amount,y.APSupplierID,y.ExpenseReportID,y.Employee,y.ReportDate,y.ExpenseDate,y.Description
FROM dbo.TotalsByGLCenter$ y
INNER JOIN (SELECT
Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,Description,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate, COUNT(*) AS CountOf
FROM dbo.TotalsByGLCenter$
GROUP BY Entity,ExpenseTypeCode,ExpenseType,LOB,Center,Location,Amount,APSupplierID,ExpenseReportID,Employee,ReportDate,ExpenseDate,Description
HAVING COUNT(*)>1
) dt ON y.Entity=dt.Entity and y.ExpenseTypeCode=dt.ExpenseTypeCode and y.ExpenseType=dt.ExpenseType and y.LOB=dt.LOB and y.Location=dt.Location and y.Center=dt.Center and y.Amount=dt.Amount and y.APSupplierID=dt.APSupplierID and y.ExpenseReportID=dt.ExpenseReportID and y.Employee=dt.Employee and y.ReportDate=dt.ReportDate and y.ExpenseDate=dt.ExpenseDate and y.Description=dt.Description
i Want Delete selected Query . how can I use delete Query Above Query Statement ?
Try this mate ,
DELETE FROM your_table WHERE id IN (
Select (
your select query
) As alias
)

#1093 - You can't specify target table 'comments' for update in FROM clause

I want to delete duplicate comments from my database
Delete FROM `comments` WHERE id IN
(
SELECT id
FROM comments
GROUP BY
COMMENT , course_id
HAVING COUNT( * ) >1
)
I got error #1093 -
You can't specify target table 'comments' for update in FROM clause.
how can I modify my query to work fine
Try aliasing the sub query:
Delete FROM `comments` WHERE id IN
(
select id from (
SELECT id
FROM comments GROUP BY `COMMENT` , course_id
HAVING COUNT( * ) >1
) as comm
)

mysql delete query with sub query not working

I try to create mysql event that should delete duplicate rows of table.
This is my query
DELETE FROM locations
WHERE id NOT IN (SELECT id
FROM locations
GROUP BY acc_id,`date`)
I got this error:
Error Code: 1093
You can't specify target table 'locations' for update in FROM clause.
How can I change the query to make it work?
In MySQL you can't delete from the same table you are selecting from. But you can trick MySQL with another subselect
DELETE FROM locations
WHERE id NOT IN
(
select * from
(
SELECT id
FROM locations
GROUP BY acc_id, `date`
) x
)
Try to provide the custom alias in your subquery you cannot directly specify the same table in update/delete
DELETE FROM locations
WHERE id NOT IN (
SELECT new_table.id FROM (
SELECT id
FROM locations
GROUP BY acc_id,`date`
) new_table
)

How to make a select statement in update?

I need to update a table, and the Where clause should contain the last (or max) from a certain column, so I made this query:
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN (
SELECT MAX(orderid)
FROM Orders);
But, for some reason I don't understand, mysql returns this error:
1093 - You can't specify target table 'Bestellingen' for update in FROM clause
I tried different queries, which aren't working either...
Can someone help??
Sorry for the crappy english
This is a MySQL limitation. (As the documentation puts it: "Currently, you cannot update a table and select from the same table in a subquery.") You can work around the limitation by writing your subquery as (SELECT * FROM (SELECT ...) t), so that MySQL will create a temporary table for you:
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN
( SELECT *
FROM ( SELECT MAX(orderid)
FROM Orders
) t
)
;
UPDATE Orders
SET Ordermethod='Pickup'
WHERE orderid IN( SELECT MAX(orderid) FROM
(
SELECT * FROM Orders
)
AS c1
)