I'm having trouble with updating final_id by selecting the highest final_id already in table and adding +1.
The query below outputs the error: "You can't specify target table 'customer_orders' for update in FROM clause" and I sadly fail to see why..
UPDATE customer_orders
SET final_id = (SELECT final_id FROM customer_orders ORDER BY final_id DESC)+1,
status = 2,
payment_id = '{$transaction_id}',
payment_type = '{$type}',
payment_reserved = '{$amount}',
payment_currency = '{$cur}',
payment_cardnopostfix = '{$postfix}',
payment_fraud_suspicious = '{$fraud}'
WHERE id = '{$order_id}'
I'm trying to set a unique increasing ID for finalized orders in my system.
I hope someone can tell me what I'm doing wrong!
Best regards
You can rewrite your query and use join
UPDATE customer_orders
INNER JOIN (SELECT IFNULL(MAX(final_id),0) as max_id FROM customer_orders)a ON(1=1)
SET final_id = a.max_id+1, status = 2, payment_id = '{$transaction_id}',
payment_type = '{$type}', payment_reserved = '{$amount}',
payment_currency = '{$cur}', payment_cardnopostfix = '{$postfix}',
payment_fraud_suspicious = '{$fraud}'
WHERE id = '{$order_id}'
change the inner query to SELECT max(final_id) FROM customer_orders
Try this:
UPDATE customer_orders
SET final_id = MT.MaxId + 1 -- use the computed max id, and increment
, status = 2
, payment_id = '{$transaction_id}'
, payment_type = '{$type}'
, payment_reserved = '{$amount}'
, payment_currency = '{$cur}'
, payment_cardnopostfix = '{$postfix}'
, payment_fraud_suspicious = '{$fraud}'
FROM customer_orders
-- include a subquery to determine the max id from the customer_orders table
-- and assign 'MT' as the name of the results table
, (SELECT MAX(final_id) as MaxId FROM customer_orders) MT
WHERE id = '{$order_id}'
Related
I am trying to update a table in MYSQL, by specifying the value of foreign keys in the table. For instance I have the tables Person, Location, Job. I have added a position column to the Job table and based on the PersonId, LocationId which are foreign keys in Job table and the pay of the job, I would like to update the position. I have tried the following SQL query to achieve this:
UPDATE Job
SET position = 'Doctor'
WHERE Job.pay = 2000 AND
Job.LocationId = (SELECT Location.LocationId FROM Location WHERE Location.area = 'NYC') AND
Job.PersonId = (SELECT Person.PersonId FROM Person WHERE Person.name = "John);
I get the error "Subquery returns more than one row". Is there a way around this? Any help would be appreciated. Thank you.
You have to get 1 record in each sub select
UPDATE Job
SET position = 'Doctor'
WHERE Job.pay = 2000 AND
Job.LocationId = (SELECT Location.LocationId FROM Location WHERE Location.area = 'NYC' LIMIT 1) AND
Job.PersonId = (SELECT Person.PersonId FROM Person WHERE Person.name = "John" LIMIT 1);
It's s because you may have two person name as john.
In that case you can use where in
Like below
UPDATE Job
SET position = 'Doctor'
WHERE Job.pay = 2000 AND
Job.LocationId = (SELECT Location.LocationId FROM Location WHERE Location.area = 'NYC') AND
Job.PersonId in (SELECT Person.PersonId FROM Person WHERE Person.name = "John" );`
Or else try to limit the sub query response to one. As below
UPDATE Job
SET position = 'Doctor'
WHERE Job.pay = 2000 AND
Job.LocationId = (SELECT Location.LocationId FROM Location WHERE Location.area = 'NYC') AND
Job.PersonId = (SELECT Person.PersonId FROM Person WHERE Person.name = "John" LIMIT 1);
im trying to complete an update in mysql where it selects only the highest import_date and i cant seem to get it to work. the below statement updates both prices
UPDATE material, pricelist
SET price = '23'
WHERE material.id = 1936 AND
material.id = pricelist.material_id AND
pricelist.import_date = (SELECT max(pricelist.import_date))
Does this do what you want?
UPDATE pricelist pl
SET pl.price = '23'
WHERE pl.material_id = 1936
ORDER BY pl.import_date DESC
LIMIT 1;
I'm aware of this:
IF EXISTS (SELECT * FROM table WHERE pk = #pk)
BEGIN
--Update
END
ELSE
BEGIN
--INSERT
END
But this works for one row. Let's say I have a table min_max M and a temp table __MinMaxImport T. I want to INSERT from T to M, but UPDATE when the pair branch_no/product already exists in M.
INSERT:
INSERT INTO min_max
(user_no, branch_no, product, min_stock, max_stock)
SELECT
#user_no, _branch_no, _product, _min_stock, _max_stock
FROM
traxs_temp..__MinMaxImport
WHERE
session_id = #session_id
UPDATE:
UPDATE min_max SET
date_time = GETDATE(),
user_no = #user_no,
min_stock = _min_stock,
max_stock = _max_stock
FROM
min_max M
INNER JOIN traxs_temp..__MinMaxImport T ON T._product = M.product
AND T._branch_no = M.branch_no
WHERE
session_id = #session_id
What is the best solution to do this? I read about MERGE but I'm not sure how to use it.
MERGE should be able to do it.
MERGE INTO min_max
USING
(
SELECT
#user_no AS _user_no, _branch_no, _product, _min_stock, _max_stock
FROM
traxs_temp..__MinMaxImport
WHERE
session_id = #session_id
) AS Src
ON
(min_max.branch_no = Src._branch_no) AND
(min_max.product = Src._product)
WHEN MATCHED THEN
UPDATE SET
date_time = GETDATE(),
user_no = Src._user_no,
min_stock = Src._min_stock,
max_stock = Src._max_stock
WHEN NOT MATCHED BY TARGET THEN
INSERT
(user_no, branch_no, product, min_stock, max_stock)
VALUES
(Src._user_no,
Src._branch_no,
Src._product,
Src._min_stock,
Src._max_stock)
;
Please could someone give me the correct syntax for below.
MySQL UPDATE tblcontact SET MainContact = 1
WHERE COUNT(tblcontact.CompanyID) = 1
GROUP BY tblcontact.CompanyID
I get the idea. You want to set the field to 1 where there is only one record. Try this:
UPDATE tblcontact c join
(select CompanyID, count(CompanyID) as cnt
from tblcontact
group by CompanyId
) cc
on c.CompanyId = cc.CompanyId and cnt = 1
SET c.MainContact = 1 ;
The following is the MS Sql server Update statement
Update
HC_TranDetails
SET
InsPayments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 2
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
Adjustments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 8
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
FROM
HC_TranDetails TD
Now i am trying the same kind of statement in mysql as follows
UPDATE claimdetails SET balanceAmount = (SELECT IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0)
FROM claimdetail CD WHERE CD.claimID = CDS.claimID)
FROM ClaimDetail CDS
But it is showing as syntax Error near 'From ClaimDetail CDS' at line 4
MySQL is squeamish about updates on the same table. The easy fix is to include an extra level of subquery. The proper fix, though, is to use a join
UPDATE claimdetails join
(select claimid, IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0) as val
from ClaimDetails
group by claimid
) agg
on claimdetails.claimid = agg.claimid
SET balanceAmount = agg.val;
You can join the table you want to update with a subquery that calculates the balance for each claimid on the other table.
By using LEFT JOIN, it will update all records on table claimdetails. A value of 0 will be updated to any non existent claimid on the subquery.
UPDATE claimdetails a
LEFT JOIN
(
SELECT claimID,
SUM(IFNULL(debitamount, 0)) - SUM(IFNULL(creditamount,0)) bal
FROM claimdetail
GROUP BY claimID
) b ON a.claimID = b.claimID
SET a.balanceAmount = IFNULL(b.bal, 0)