Take the result of a query and write it to a field in a table

Hey guys.
I'm racking my brain trying to create select and insert logic.

What I need is:

  1. Query a table according to a condition (ID)
  2. Take the result of this query and save it in a table. But here is my problem:
    Let's assume that the query returns 3 results that have post_id 179.

id 1. meta_value = i:1;i:1722384000;
id 2. meta_value = i:2;i:1722470400;
id 3. meta_value = i:3;i:1722556800;

I need to take the results from the meta_value field and somehow concatenate them, group them, I don't know... into a single result that looks like this:
i:1;i:1722384000;i:2;i:1722470400;i:3;i:1722556800;

  1. Take this line (grouped result) and apply this mask:
    a:3:{i:1;i:1722384000;i:2;i:1722470400;i:3;i:1722556800;}
    Where a:3 (the number 3 is the number of results found, here in this case there are 3 but it could be 2 or 5, 6... This will depend on the number of results found in the query)

What I have achieved so far is in the following image:

Why not store the $_POST object in a JSON/JSONB column in your database. Fastest, simplest and easily accessible afterwards for post-processing

1 Like

You can do this with the dynamic data picker as well, but here's the code part:

'a:'+query.data.count()+':{'+query.data.join('', 'meta_value')+'}'

this should do the job for the format you explained in the screenshot.

1 Like

Hello @Teodor . Thank you for your help.
I was able to return the number of records correctly but I cannot update a table with the returned data.
If the "postmeta_final" table does not have the corresponding ID (example: 179) I can do an INSERT but if it already contains data with the specific ID (example: 179) I cannot do the UPDATE.

After repeating with this data: 'a:'+query.data.count()+':{'+query.data.join('', 'meta_value')+'}' I created a query that checks if there is data (with ID 179 for example) in the "postmeta_final" table.
If it contains data I need to update it. If not, do an INSERT.

sorry i don't quite understand what value do you check and how do you filter your query.

Hello, @Teodor
Query1 searches the postmeta_final table and filters by get (id=179 for example).
Soon after, there is the condition:
If you have data in the postmeta_final table with ID 179, for example, you do an UPDATE if you don't do an INSERT.
UPDATE:

INSERT:

INSERT works fine but UPDATE does not update the postmeta_final table.

What’s not working exactly with the update step and how is this related to your original question about joining the values?

Hello, @Teodor
OK, let's go.
In the first question I applied the code: 'a:'+query.data.count()+':{'+query.data.join('', 'meta_value')+'}' and it returned exactly what I need . The result united into a single one. I lose.
Now if I update the postmeta_temp table, the metaFinal result continues with the 3 results from the previous query.

The postmeta_temp table with 3 results

The postmeta_temp table with 1 result

My question is: why does the metaFinal result continue to show 3 if you only have one record.

Hey Marcos,

It looks like you are getting wrong results (wrong condition? wrong datasourse?)

Can you screenshot that query1 to see ow it is build?

Hello, @famousmag
Query1 is in the following image

This query searches for records in the postmeta_final table and filters by the ID passed in the URL.
So I create a condition if there are already records with the URL ID I do an UPDATE if not I do an INSERT..
INSERT works but update does nothing.

At this moment, the postmeta_final table has this record:
a:3:{i:1;i:1722556800;i:2;i:1722643200;i:3;i:1722729600;}
Notice that you have 3 records grouped together...
I need to update this table with the result I get in the first query: a:1:{i:1;i:1722384000;}.

hey brother,

It's difficult for us to understand where is the mistake...

So, the insert is working fine because you don't filter with a value.
But when you try to update, obviously a wrong value is passed in your condition and the record to be updated is not found...

I suggest to use setValue steps with output checked so you know what value is being proccessed:

  1. first step in your api, add a setValue with the post/get values and add an additional string so you know what is what...
    for example
    setValue getID = 'getID=' + $_GET.id

  2. right after the paged query, add another setValue with the query result
    setValue queryID = 'query.data.id=' + query.data[0].id

and so on...

I just want to say that at query1 you have id = 8 and post_id = 179
Maybe fron the previous wrong updates you have done, there are more than one records with post_id=179

It could be easier if you can toggle the view, and expand the input $_GET values to see everything and we go from there

1 Like

Please post a screenshot of how’s your update step setup - so we can see the values and the condition you’re using.

Hello @famousmag .
Here is the Set Value and the result.
Below are the update screens.

Hello @Teodor
Below are the update screens.

But what are you doing exactly, why are you updating the column you are using in the conditions tab to filter the records?
And what is the EXACT value your $_GET.id returns? Is it getID=179 or just 79?

Sorry i am getting more and more confused after every new screenshot you post :slight_smile:

1 Like

Sorry I am not shure about this. As I can see the query1 is doing what is intended to do. It searches the postmeta_final for the existing data.

however I can't see any update query her. You have to do a update after you have found the right postmaster_final data to update. then you can search with query1 again and get the new result.

And it is always best to update the database with the Id field if it is a single record you are getting as a result.

1 Like

Hello.
I managed to resolve it.
The problem was with the ID to be updated in the postmeta_final table.
I was using it as a condition to update the GET ID.
But I got it and it's working perfectly.
I thank everyone for the help.

1 Like

Hello Teodor.
I managed to resolve it.
The problem was with the ID to be updated in the postmeta_final table.
I was using it as a condition to update the GET ID.
But I got it and it's working perfectly.
I thank everyone for the help.

Hello.
I managed to resolve it.
It was the ID that I was using as a condition to update the data.

1 Like