CTE the DELETE is not updateable [duplicate] - mysql

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 1 year ago.
I am new to SQL and using MYSQL Workbench.
In this query, I am trying to find duplicate values and then delete the duplicates(anything > 1)
But I am getting the error "The target table RowNumCTE of the DELETE is not updatable"
-- Remove duplicates
With RowNumCTE as(
select *,
row_number() over (
partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
order by UniqueID) row_num
from portfolioproject.nashvillehousing)
delete
from RowNumCTE
where row_num > 1;
Your guidance will be highly appreciated.

You can't delete from a CTE on MySQL 8+, as you might have been able to do on SQL Server. But, we can rephrase your logic as follows:
DELETE
FROM portfolioproject.nashvillehousing p1
WHERE EXISTS (SELECT 1 FROM portfolioproject.nashvillehousing p2
WHERE p2.ParcelID = p1.ParcelID AND
p2.PropertyAddress = p1.PropertyAddress AND
p2.SalePrice = p1.SalePrice AND
p2.SaleDate = p1.SaleDate AND
p2.LegalReference = p1.LegalReference AND
p2.UniqueID < p1.UniqueID);

Related

While *Delete* the multiple rows using column value, getting "update you can't specify target table for update in from clause" this error in mysql [duplicate]

This question already has answers here:
You can't specify target table for update in FROM clause
(11 answers)
Closed 8 months ago.
I'm using the below DELETE query.
DELETE FROM tableAA WHERE id IN (SELECT id FROM tableAA WHERE coreID IN (SELECT fill_ID FROM tableBBB) and columnData= 'data');
but I am getting this "update you can't specify target table for update in from clause" error.
Try with this one:
DELETE FROM tableAA
WHERE coreID IN (SELECT fill_ID FROM tableBBB)
AND columnData = 'data';
Sorry, i can not check my example right now:
DELETE FROM tableAA LEFT JOIN tableAA.id = fill_ID.tableBBB WHERE fill_ID NOT IS NULL and columnData = data;

Delete every value that have a id which is not repeated in sql [duplicate]

This question already has answers here:
You can't specify target table for update in FROM clause
(11 answers)
MySQL DELETE FROM with subquery as condition
(9 answers)
Closed 9 months ago.
I'm doing a function that removes a project, and when it removes its tags are removed if there isn't another projects using the same tag.
So when I select the project it sends this:
delete from Project_Tag where (TagId, ProjectId) in (
select q.ProjectId, q.TagId from Project_Tag q
where q.TagId in (select g.TagId
from Project_Tag g where ProjectId = <ID OF THE PROJECT>)
group by q.TagId having count(q.TagId) = 1
);
But it gives an error: You can't specify target table "Project_Tag" for update in FROM clause
This select is working, but I can't delete it
select q.ProjectId, q.TagId from Project_Tag q
where q.TagId in (select g.TagId
from Project_Tag g where ProjectId = <ID OF THE PROJECT>)
group by q.TagId having count(q.TagId) = 1
Is there another way to solve this? I'm newbie yet :P

MySql : Updating all except the first row [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I went through the previous answer like this, but it gives me the following error : You can't specify target table 'table_name' for update in FROM clause.
I have a table with say 3 columns (id -> auto increment primary id) :
id, roll_no and attendance
And for selected roll numbers having many entries each, except the first entry I want to update all entry attendance field as P.
The query which I wrote is following :
UPDATE tbl_class_attendance
set attendance = 'P'
where id NOT IN
(Select min(id)
from tbl_class_attendance
WHERE roll_no IN ('25', '45', '55')
GROUP
BY roll_no;
But it gives me the above error.
I also went through other answers asking to use two select queries but there the answer I didn't find completely easy to understand as well as difficulty in executing for my selected list of roll numbers.
So, is there a way to update?
EDIT : Answer given below
UPDATE tbl_class_attendance t1
LEFT JOIN ( SELECT t2.roll_no, MIN(t2.id) id
FROM tbl_class_attendance t2
WHERE t2.roll_no IN ('25', '45', '55')
GROUP BY t2.roll_no ) t3 USING (roll_no, id)
SET t1.attendance = 'P'
WHERE t3.id IS NULL;
Got it working by following :
Update table SET a=value WHERE x IN
(Select * from (select x from table where condition) as t)
Credit : https://stackoverflow.com/a/43610081/6366458

Error updating record with sub query [duplicate]

This question already has answers here:
MySQL Error 1093 - Can't specify target table for update in FROM clause
(16 answers)
Closed 2 years ago.
I am updating record on the base of sub query but it giving me error
you can't specify target table for update in from clause
my query is
UPDATE paymentinfo set customer_id =
(
SELECT transation.transactionid
FROM paymenttransaction AS transation
LEFT JOIN paymentinfo as payment
ON (transation.paymentinfoid=payment.paymentinfoid)
where payment.hash="0b576d33c57484692131471a847eab7c"
)
WHERE hash="0b576d33c57484692131471a847eab7c"
where am i wrong and what will be perfect solution for that problem
You are updating the table 'paymentinfo' also at same time you are using this table for selection in subquery .
Please break this query in two parts and it will work .
I think it is simplest (in your case) to use the double subquery method:
UPDATE paymentinfo
SET customer_id = (SELECT transactionid
FROM (SELECT t.transactionid
FROM paymenttransaction pt LEFT JOIN
paymentinfo pi
ON t.paymentinfoid = pi.paymentinfoid
WHERE p.hash = '0b576d33c57484692131471a847eab7c'
) t
)
WHERE hash = '0b576d33c57484692131471a847eab7c';
Usually, you want to switch these to use JOIN, but I think that is a bit complicated in this case.

DELETE from TABLE WHERE? [duplicate]

This question already has answers here:
Remove duplicate rows in MySQL
(26 answers)
Closed 9 years ago.
I have a table and I need the correct statement to delete duplicate records. I want to achieve this by deleting rows in the table where multiple column values equal those of other records in the same table. I can do simple delete from [table] where [col] = [val] but I have no idea how to do this. Can you provide a template that I can work from?
Thanks.
delete from [table]
where [col] not in
(
select * from
(
select min([col])
from [table]
group by [col]
having count(distinct [val]) <> count([val])
) x
)
If you select from a table you are deleting from in MySQL then you have to use a subquery to hide that. That is why I used the select * from (...) x
Assuming that the original key's id is say 7,
DELETE FROM [table] WHERE [col1] = [val1] AND [col2] = [val2] .... AND [id] != 7;