I have to delete a row with max(id) from a table in MySQL.
I am using query::
DELETE
FROM master
WHERE id=(
SELECT MAX(id)
FROM master)
but getting error No. 1093.
Can anybody please help me??
You can't specify target table for update in FROM clause.
you can delete the last row as mentioned below.
DELETE FROM master ORDER BY id DESC LIMIT 1
You cant modify the same table from which you are selecting the data in subquery.
Try this -
DELETE m.*
FROM master m
WHERE id IN (SELECT id_temp from(
SELECT MAX(id) as id_temp
FROM master) x)
You can't specify target table for Delete in FROM clause
Try this
DELETE FROM master
WHERE id IN (SELECT A.MAXid FROM
(SELECT MAX(id) as MAXid FROM master) A
)
DELETE FROM Test WHERE id IN(SELECT MAX(id) FROM Test);
This seems to be cross SQL vendor friendly. The LIMIT option in MySQL becomes this in SQL SERVER.
DELETE FROM Test WHERE id IN(SELECT TOP 1 id FROM Test ORDER BY id DESC );
See here
Related
if I want to delete some data in mysql, can I use
delete from table where xxx='xxx' and indexID not in (select * table where xxx='xxx limit 3)??
because when I used this code, I got the error.
I know how to working in mssql,
DELETE FROM table WHERE xxx = 'xxx' AND indexID NOT IN (SELECT TOP(3) indexID FROM table WHERE xxx = 'xxx' ORDER BY check_date DESC
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.
Following is my query, I am trying to update recods but I get an error:
you can't specify target table for update in from clause
UPDATE user_payment_info
SET
ammount='110',
status='failed',
transaction_id='0'
WHERE
id=(SELECT id
FROM user_payment_info
WHERE cust_id='771'
ORDER BY id DESC
LIMIT 1)
how can update record with getting id from same table
how can i resolve these mysql error
you can't specify target table for update in from clause
can some body help me to do these.
Try this instead:
UPDATE user_payment_info AS t1
INNER JOIN
(
SELECT MAX(id) AS MaxId
FROM user_payment_info
WHERE cust_id='771'
) AS t2 ON t1.id = t2.MaxId
SET t1.ammount='110',
t1.status='failed',
t1.transaction_id='0';
You don't have to use a subquery, you could use an UPDATE query with order by and LIMIT 1:
UPDATE
user_payment_info
SET
ammount='110',
status='failed',
transaction_id='0'
WHERE
cust_id='771'
ORDER BY
id DESC
LIMIT 1
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
)
I'd like to select all records from a table (names) where lastname is not unique. Preferably I would like to delete all records that are duplicates.
How would this be done? Assume that I don't want to rerun one query multiple times until it quits.
To find which lastnames have duplicates:
SELECT lastname, COUNT(lastname) AS rowcount
FROM table
GROUP BY lastname
HAVING rowcount > 1
To delete one of the duplicates of all the last names. Run until it doesn't do anything. Not very graceful.
DELETE FROM table
WHERE id IN (SELECT id
FROM (SELECT * FROM table) AS t
GROUP BY lastname
HAVING COUNT(lastname) > 1)
The fastest and easiest way to delete duplicate records is my issuing a very simple command.
ALTER IGNORE TABLE [TABLENAME] ADD UNIQUE INDEX UNIQUE_INDEX ([FIELDNAME])
This will lock the table, if this is an issue, try:
delete t1 from table1 t1, table2 t2
where table1.duplicate_field= table2.duplicate_field (add more if need ie. and table.duplicate_field2=table2.duplicate_field2)
and table1.unique_field > table2.unique_field
and breakup into ranges to run faster
dup How can I remove duplicate rows?
DELETE names
FROM names
LEFT OUTER JOIN (
SELECT MIN(RowId) as RowId, lastname
FROM names
GROUP BY lastname
) as KeepRows ON
names.lastname = KeepRows.lastname
WHERE
KeepRows.RowId IS NULL
assumption: you have an RowId column
SELECT COUNT(*) as mycountvar FROM names GROUP BY lastname WHERE mycountvar > 1;
and then
DELETE FROM names WHERE lastname = '$mylastnamevar' LIMIT $mycountvar-1
but: why don't you just flag the fielt "lastname" als unique, so it isn't possible that duplicates can come in?