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.
Related
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;
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
This question already has answers here:
mysql update column with value from another table
(9 answers)
Update mysql table with data from another table
(5 answers)
Closed 5 years ago.
hi here i have two tables named as test and test2.
test is as follows
test2 is as follows
i am using below sql code to get the below output.
UPDATE `test`
SET `availability` = 'ok'
WHERE
`id` = '(SELECT
test.id
FROM
test2
INNER JOIN test ON test.id = test2.PId)';
I requires below output. but it outcomes no any output. kindly help. any mistakes done by my end or if there is any best mothod to get below output, kindly mention
I think you're looking for something along the lines of
UPDATE test
INNER JOIN test2 on test.id = test2.PId
SET test.availability = 'OK'
Sounds like you don't need an update, just a query with a join:
SELECT test.id, test.name, CASE WHEN teste2.pid IS NOT NULL THEN 'OK' END
FROM test
JOIN test2 ON test.id = test2.pid
I am not sure if this will work.but hey we all try here right..
UPDATE `test`
SET `availability` = 'ok'
WHERE
`id` in '(SELECT
PId from test2)';
I don't get , why do you need inner join.You only need those id's that are present in test2 table,then just take it.
although i have used 'in' keyword,try with '=' also but i doubt it will work because inner query is returning a list of id's.thanks.
Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)
I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)