Incorrect usage of UPDATE and LIMIT - mysql

When I execute the following query:
UPDATE `table1`
INNER JOIN Address ON Address.Mobile = table1.number
LEFT JOIN tps ON tps.number = table1.number
SET table1.export = '2015-03-31'
WHERE Address.Surname != '' and tps.number is null AND table1.export = '0000-00-00'
limit 100000
I get error:
Incorrect usage of UPDATE and LIMIT
I need to use Limit when using Update join. How to solve this issue?

Think it is objecting to the use of order / limit on a multi table update statement.
I would suggest trying to to do an update where the key field is in the results of a sub query that returns the limited set of records. One problem here is that MySQL will not allow you to update a table that is also in the sub query, but you can normally get around this by having a 2nd containing sub query.
Something like this:-
UPDATE table1
SET table1.export = '2015-03-31'
WHERE table1.number IN
(
SELECT number
FROM
(
SELECT table1.number
FROM `table1`
INNER JOIN Address ON Address.Mobile = table1.number
LEFT JOIN tps ON tps.number = table1.number
WHERE Address.Surname != '' and tps.number is null AND table1.export = '0000-00-00'
limit 100000
) sub1
) sub2

Related

Using `limit` with an update sql query

I have the following query where I want to limit the number of rows it updates for the subs table. It keeps hitting an error though, where am I going wrong?
UPDATE subs t1
INNER JOIN imp_subscriptionlog t2
ON t1.subscription_id = t2.subscription_id
SET t1.cancellation_date = t2.date
WHERE t2.event = 'subscription_cancelled'
LIMIT 35
This is the error:
Incorrect usage of UPDATE and LIMIT
Error code 1221.
LIMIT is allowed in single-table updates only, as explained in the documentation:
For the single-table syntax, [...] if the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated.
For multiple-table syntax, ORDER BY and LIMIT cannot be used.
You can rewrite the query to use a correlated subquery instead of a join:
update subs
set cancellation_date = (
select t2.date
from imp_subscriptionlog t2
where t2.subscription_id = subs.subscription_id and t2.event = 'subscription_cancelled'
)
order by ???
limit 35
Notes:
you should be specifying an order by clause in the query, otherwise it is undefined which rows will actually be updated
the query implicitly assumes that there is always just one matching row in imp_subscriptionlog for each row in subs; if that's not the case, then you must order by and limit 1 in the subquery as well, or use aggregation
we can also ensure that there is a match before updating by adding a where clause to the query
Here is a "safer" version of the query, that updates to the maximum date value available in the other table, while not modifying rows that have no match:
update subs
set cancellation_date = (
select max(t2.date)
from imp_subscriptionlog t2
where t2.subscription_id = subs.subscription_id and t2.event = 'subscription_cancelled'
)
where exists (
select 1
from imp_subscriptionlog t2
where t2.subscription_id = subs.subscription_id and t2.event = 'subscription_cancelled'
)
order by ???
limit 35
Update sub1
Inner join
(
//your select statement another table
//condition
//Now use limit
Limit 10
)
On
sub.data = table.date
set

Can't specify target table for update in FROM clause error

Can some help me to solve this query
UPDATE
LoadBal_WebAPI_RequestDetails
SET
status='vamsi'
WHERE
requestID in (select `lb`.`requestId` AS `requestID` from (`LoadBal_WebAPI_RequestDetails` `lb` join `Global_AR_Processes` `ar` on((`ar`.`processId` = `lb`.`processRegistryId`))) where ((`lb`.`status` = 'Received') and (`ar`.`isProcessAvailable` = 1)) order by `lb`.`lastUpdatedDateTime` )
LIMIT 1;
I am getting error 1093 - Can't specify target table for update in FROM clause
I am new in query writing, so suggest me in solving the error
Thanks
Try the following query:
UPDATE
LoadBal_WebAPI_RequestDetails
SET
status='vamsi'
WHERE
requestID in (select t1.requestID FROM
(select requestId, processRegistryId from LoadBal_WebAPI_RequestDetails where `status` = 'Received' order by `lastUpdatedDateTime`) t1 JOIN
`Global_AR_Processes` `ar` on `ar`.`processId` = t1.processRegistryId and `ar`.`isProcessAvailable` = 1)
LIMIT 1;
I think you should use nested table as select from your select statement like this
UPDATE tbl SET col = (
SELECT ... FROM (SELECT.... FROM) AS x);

How to execute update join query?

How do we run these types of queries in MySQL?
How to execute these types of queries how we run in MySQL, i.e. update join query?
UPDATE file_master t1,users t2 SET
t1.Status = "cancel",
t1.is_credit_revers=1,
t1.is_credit_reversed=1,
t1.t_reversal=t1.credit,
t2.credit=t1.credit+
(
select sum(t1.credit) from file_master where FileID in(7,6,5)
)
WHERE t1.FileID in(7,6,5)
and t1.CRN=t2.id
and t1.CRN=1 ;
1093 - Table 't1' is specified twice, both as a target for 'UPDATE' and as a separate source for data
MySQL objects to updating a table which is also in a sub query
You can sometimes hide the sub query within another sub query to get around this.
As such try something like this:-
UPDATE file_master t1
INNER JOIN users t2
ON t1.CRN = t2.id
CROSS JOIN
(
SELECT credit_sum
FROM
(
SELECT SUM(credit) AS credit_sum
FROM file_master
WHERE FileID IN(7,6,5)
) t3
) t4
SET t1.Status = "cancel",
t1.is_credit_revers = 1,
t1.is_credit_reversed = 1,
t1.t_reversal = t1.credit,
t2.credit = t1.credit + t4.credit_sum
WHERE t1.FileID in(7,6,5)
AND t1.CRN = 1 ;

Mysql Update wtih Join and limit

I need to update table using join and limit. I construct this query
UPDATE table1
JOIN table2 AS b
ON table1.id = b.id
SET
table1.username = b.post_username
WHERE b.post_username != ''
AND table1.username = ''
LIMIT 10
unfortunaetly I recive error:
Error Code: 1221
Incorrect usage of UPDATE and LIMIT
How can I solve this?
I'm afraid you can't do that:
If you read the documentation it says:
With no WHERE clause, all rows are updated. If the ORDER BY clause is
specified, the rows are updated in the order that is specified. The
LIMIT clause places a limit on the number of rows that can be updated.
For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case,
ORDER BY and LIMIT cannot be used.
So you can't do that in your query.
hey just Delete LIMIT 10 from your code
You can use following query syntax:
update work_to_do as target
inner join (
select w. client, work_unit
from work_to_do as w
inner join eligible_client as e on e.client = w.client
where processor = 0
order by priority desc
limit 10
) as source on source.client = target.client
and source.work_unit = target.work_unit
set processor = #process_id;

Can I combine 2 tables in MySQL and update a field?

Ok so what I need to do here is, take the sum of all the data with a specific name in the first table.
SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian'
then in a DIFFERENT table I need to update the Total_DKP with the sum of the previous table
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
I tried the following solution and a few others but I'm not getting any working results
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
set Total_DKP = (SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian')
To change data in a row, use the UPDATE statement:
UPDATE Characters AS c
SET c.Total_DKP = ( SELECT SUM(a.DKP_Change)
FROM Attendance AS a
WHERE a.Name = 'harrian'
)
WHERE c.Name = 'harrian' ;
UPDATE `Characters`
SET Total_DKP = (SELECT SUM(DKP_Change) FROM `Attendance` WHERE Name='harrian')
Try the following please.
update t1 set t1.secondcolumn =
(SELECT sum(blah) as blahsum
FROM t1 b
where b.name = 'harran'
)
WHERE t1.name = 'harran'
;