MySql : Updating all except the first row [duplicate] - mysql

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

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;

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.

mysql Error Code: 1093. You can't specify target table 'S' for update in FROM clause

I know there are answers on this question , but I can't understand as their question code is not same with mine.
I have code here:
UPDATE S
SET city=(SELECT city FROM S WHERE s_name='Adams')
WHERE s_name='Smith';
I tried to modify code according to the answers from other question:
UPDATE S
SET city=(SELECT city FROM (SELECT* FROM S) AS temta WHERE s_name='Adams')
WHERE s_name='Smith';
Even though the query is successful but I get null value, the value is not updated as I expected.
have you tryed so:
UPDATE S
SET city=(SELECT city FROM (SELECT * FROM S WHERE s_name='Adams') AS temta )
WHERE s_name='Smith';
OR
UPDATE S
SET city=(SELECT c FROM (SELECT city as c FROM S WHERE s_name='Adams') AS temta )
WHERE s_name='Smith';
NOTE: are you sure that subquery result is only one row?

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;

Get Identity after selecting from distinct result [duplicate]

This question already has answers here:
Get Identity after selecting from distinct result
(3 answers)
Closed 10 years ago.
i have 2 tables ( Model_Table , Items_Table)
Model_Tabl ( ID, ModelName, ModelQuantity)
Items_Tabl ( I_Code, IName, ID)
after inserting new row into (Model_Table) - Triggers insert multi row into (Items_Table) Depend on ModelQuantity from (Model_Table) , and until now its work fine
I Created
"select distinct ModelName , Sum(ModelQuantity) group by ModelName"
and i got result fine
My question is :
When i select model name from (DISTINCT) query i want to know which (ID) I selected from (Model_Table)
(Model_ID) to (ModelName) = 1 to many
ty
just select also the id ,try this
select id, ModelName , Sum(ModelQuantity)
from Model_Tabl group by ModelName
you dont need DISTINCT because you already making group by and they do same work