I hope you can help me again, thanks already for pointing me to the right direction with creating the check digit for the new IBAN in Germany. I am now trying to update our membership database with the newly calculated BIC and IBAN but seem to have a problem with the UPDATE statement on the MariaDB MySQL database, despite the fact that I think I got the syntax right.
All I am trying to do is set the two fields "konto_bic" and "konto_iban" in the table "mitglieder" from the SELECT statement which creates a temporary table called b with the columns "id", "bic" and "iban". The "id" is the same in the two tables.
Here is my first try:
update a
set a.`konto_bic` = b.`BIC`, a.`konto_iban` = b.`IBAN`
from `mitglieder` a
INNER JOIN (SELECT m.`id`, m.`nachname`, m.`vorname`, m.`konto_bank`, m.`konto_blz`, m.`konto_nummer`, k.`bic` AS 'BIC', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'IBAN'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`
ORDER BY m.`nachname`, m.`vorname`) b
ON a.`id` = b.`id`
However, this produced an error and I tried this instead:
update `mitglieder` a
set a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
FROM (SELECT m.`id` as 'id', k.`bic` as 'bic', CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k
ON m.`konto_blz` = k.`blz`) b
WHERE a.`id` = b.`id`
That also did not get me any further (error from DB).
Can anyone see what my syntax error might be?
Thank you in advance for your help
Stephan
Try below SQL
UPDATE `mitglieder` a,
(SELECT m.`id` AS 'id',
k.`bic` AS 'bic',
CONCAT('DE',LPAD(98-MOD(CONVERT(CONCAT(m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0'),'1314','00'), decimal(24)),97),2,'0'),m.`konto_blz`,LPAD(m.`konto_nummer`,10,'0')) AS 'iban'
FROM `mitglieder` m
LEFT JOIN `konvert_bic_blz` k ON m.`konto_blz` = k.`blz`) b
SET a.`konto_bic` = b.`bic`, a.`konto_iban` = b.`iban`
WHERE a.`id` = b.`id`
UPDATE Syntax
http://dev.mysql.com/doc/refman/5.0/en/update.html
I tried this on MariaDB 10.2.6 :
SET sql_mode = 'ANSI_QUOTES'
UPDATE "test"."user" AS "a"
INNER JOIN "test"."user_profile" AS "c" ON "c".user_id = "a".id
INNER JOIN "test"."profile" AS "d" ON "d".id = "c".profile_id
SET "a".firstname = 'laurent'
WHERE "a".id = 3;
And it works :)
Related
I'm trying to set a post to trash based on the title from another column in another table. Below is the code I have been trying to use, but so far the solution escapes me:
UPDATE egDuqUe_5_posts
SET egDuqUe_5_posts.post_status = 'trash'
FROM
egDuqUe_5_posts
INNER JOIN
egDuqUe_7_posts.id = egDuqUe_7_wc_order_coupon_lookup.coupon_id
WHERE
egDuqUe_5_posts.post_title = egDuqUe_7_posts.post_title
After a cup of tea, I came to this solution:
UPDATE egDuqUe_5_posts m
JOIN
egDuqUe_7_posts s
ON m.post_title = s.post_title
JOIN
egDuqUe_7_wc_order_coupon_lookup c
ON c.coupon_id = s.ID
SET m.post_status = 'trash'
WHERE m.post_type ='shop_coupon' && m.post_title = s.post_title
I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id
I have the following code that produces a list of order numbers and values...
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`;
I need to update a table called matcontctsafter which has an OrderNo field and currently blank InvoiceAmount column that I need the relative SUM(v.UnitPrice) in.
Can anybody help me construct the UPDATE clause?
UPDATE matcontctsafter m
INNER JOIN (
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`) InvoiceAmount
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`
) sq ON m.OrderNo = sq.OrderNo
SET m.InvoiceAmount = sq.InvoiceAmount;
UPDATE matcontctsafter m SET m.InvoiceAmount = (SELECT
SUM(v.UnitPrice)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.VMainID = d.MainID
WHERE m.OrderNo = d.OrderNo);
I have a problem with updating records in my table. I am doing research all day but it is just beyond me.
Basics: I have two tables
TABLE1
TABLE2
I need to update nr_g from TABLE1 with id from TABLE2 but only where 'kraj' 'region' 'nazwa_hotelu' from TABLE1 is equal 'country' 'region' 'hotelName' from TABLE2
my trying so far:
UPDATE merlinx u
LEFT JOIN
merlinx_new s ON u.nr_g != s.id
SET
u.nr_g = s.id
WHERE
u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region
That is updating me only 4 rows... and 1592 are unsafe statements
another shot of mine:
UPDATE merlinx_merged
SET
nr_g = (SELECT
merlinx_new.id
FROM
merlinx_new
INNER JOIN
merlinx_merged
WHERE
merlinx_new.country = merlinx_merged.kraj
AND merlinx_new.hotelName = merlinx_merged.nazwa_hotelu
AND merlinx_new.region = merlinx_merged.region)
And that is just throwing errors.
My mind is fried after 8 hours wasted on it. Help is much appreciated.
I think your issue is in your join statement. You have
LEFT JOIN merlinx_new s ON u.nr_g != s.id
You shouldn't have to have the ON criteria in that (if I'm understanding your question correctly).
This should do the trick if you want to overwrite merlinx.nr_g with the value from merlinx_new.id if all of your criteria matches in the WHERE clause.
UPDATE merlinx u, merlinx_new s
SET u.nr_g = s.id
WHERE u.kraj = s.country AND u.nazwa_hotelu = s.hotelName AND u.region = s.region
I want to perform an SQL update
UPDATE incCustomer SET etaxCode = 'HST' WHERE
only on records where this is true.
select etaxCode
from incCustomer
left join incAddress on incCustomer.iAddressId = incAddress.iId
left join incProvince on incAddress.iProvinceStateId = incProvince.iId
where incAddress.iProvinceStateId in ( 2 , 3 , 4 , 6 , 9 )
I don't think that this is possible with ANSI SQL, but can it be done in MySQL?
MySQL UPDATE syntax supports joins in both ANSI-89 and ANSI-92 syntax.
Use:
UPDATE INCCUSTOMER c
LEFT JOIN INCADDRESS a ON a.iid = c.iaddressid
LEFT JOIN INCPROVINCE p ON p.iid = a.iprovincestateid
SET c.etaxcode = 'HST'
WHERE a.iProvinceStateId IN (2,3,4,6,9)
I don't see the point of LEFT JOINing to the province table - I think your UPDATE could be written as:
UPDATE INCCUSTOMER
SET c.etaxcode = 'HST'
WHERE EXISTS(SELECT NULL
FROM INCADDRESS a
WHERE a.iid = INCCUSTOMER.iaddressid
AND a.iProvinceStateId IN (2,3,4,6,9))