UPDATE

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
calvin3pl
Posts: 6
Joined: 2016-04-14 00:14

UPDATE

Post by calvin3pl » 2023-09-29 21:52

Hi,

I'm working with version 23.15.1484. I have the below in the calculated field. I would like to have the 'ArticleDocument' number that matches the 'Reference' number returned in the 'CKG" field. It's not working, any help is appreciated. See attachment for example.

UPDATE `MB51_NOS` t1
JOIN `MB51_NOS` t2 ON t1.`Reference` = t2.`ArticleDocument`
SET t1.`CKG` = t2.`ArticleDocument`
WHERE t1.`Reference` IS NOT NULL AND t1.`Reference` != '';

Thank you.
Attachments
Screenshot 2023-09-29 163710.png
Screenshot 2023-09-29 163710.png (15.87 KiB) Viewed 3403 times

calvin3pl
Posts: 6
Joined: 2016-04-14 00:14

Re: UPDATE

Post by calvin3pl » 2023-09-30 02:59

Hi, looks like I had to use 'SELECT', to prevent the php errors and allow other queries to run, see below.

SELECT t1.`ArticleDocument`, t1.`Reference`, t2.`ArticleDocument` AS 'CKG_to_be_set'
FROM `MB51_NOS` t1
LEFT JOIN `MB51_NOS` t2 ON t1.`Reference` = t2.`ArticleDocument`
WHERE t1.`Reference` IS NOT NULL AND t1.`Reference` != '';

The issue I'm getting now are duplicates, see below. Any help is appreciated
AppGini1.png
AppGini1.png (216.64 KiB) Viewed 3392 times

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1944
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: UPDATE

Post by jsetzer » 2023-09-30 05:19

The SQL command for calculated field should update exactly one row. You have to append one more condition to your WHERE part:

Code: Select all

AND TABLENAME.PKCOLUMNNAME = '%ID%'
Replace tablename and pkcolumnname.

%ID% will be replaced by AppGini automatically at runtime.

Additionally, if your SELECT returns more than one value, the field will be updated with the first value. So, there is no need to add more than one field.

Having said this, instead of using calculated fields consider changing the TABLENAME_after_update hook (and perhaps after_insert) and do all necessary database updates in PHP. There you are free to update whatever you want, even multiple fields at once and even for the whole table or parts of a table.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 25.10 + all AppGini Helper tools

calvin3pl
Posts: 6
Joined: 2016-04-14 00:14

Re: UPDATE

Post by calvin3pl » 2023-09-30 12:12

Thank you, Jsetzer! An AppGini Super Hero you are! The below worked perfect and I'll look at the hook recommendation.

SELECT t1.`ArticleDocument`, t1.`Reference`, t2.`ArticleDocument` AS 'CKG_to_be_set'
FROM `MB51_NOS` t1
LEFT JOIN `MB51_NOS` t2 ON t1.`Reference` = t2.`ArticleDocument`
WHERE t1.`Reference` IS NOT NULL
AND t1.`Reference` != ''
AND t1.MB51_NOS_Key = '%ID%';

Have a great day!

Post Reply