Search on array of strings column in MYSQL - mysql

I have a column name "multiselect_value" in which i am storing data as array of strings. Here is a sample
multiselect_value
["Books","Hockey","Movies"]
["Cricket","Books","Movies"]
Now i have a input value like ["Hockey","Books"] and i want to get those records in which this value exist in "multiselect_value" column
Here is what i have tried
SELECT user_id,multiselect_value
FROM user_custom_field_data
WHERE FIND_IN_SET("Books",multiselect_value) > 0;

Related

Values from xml data field in mysql

i would like to know if there is a query to select values from all of my xml data fields. There are around 1k rows which has xml data. All of them has almost the same data structure. With extract value i was able to extract one data field but at the point where more than one row is part of my subquery it breaks.
Here is an example xml data inside my db:
<EDLXML version="1.0.0" type="variable">
<properties id="template_variables">
<deliveredDuration>4444</deliveredDuration>
<deliveredNum>1</deliveredNum>
<comment/>
<projectname>cdfkusen</projectname>
<name>kral_schalke_trenink</name>
<order_id>372846</order_id>
<cutlistId>2763_ID</cutlistId>
<bcutlistId>51ddgf7a6-1268-1gdfged-95e6-5254000e8e1a</bcutlistId>
<num>1</num>
<duration>177760</duration>
<quotaRelevantDuration>0</quotaRelevantDuration>
<organisationUid>OrgName</organisationUid>
<organisationQuota>333221233</organisationQuota>
<organisationUsedQuota>123</organisationUsedQuota>
<organisationContingentIrrelevantQuotaUsed>54</organisationContingentIrrelevantQuotaUsed>
<userDbId>7xxxx84-eb9b-11fdsb-9ddd1-52cccccde1a</userDbId>
<userId>xxxx</userId>
<userRights>RH_DBC</userRights>
<firstName>DThom</firstName>
<lastName>Test</lastName>
<userMail>xxx#ccc.cz</userMail>
<language>English</language>
<orderTimestamp>1659448080</orderTimestamp>
<stitching>false</stitching>
<transcode>NO</transcode>
<destination>Standard</destination>
<collaboration>private</collaboration>
<premiumUser>false</premiumUser>
<priority>normal</priority>
<userMail2>xxx#ccc.cz</userMail2>
<cutlistItems>
<cutListId>125124_KFC</cutListId>
<cutListItemId cutlistItemDeliveryStatus="&#10004" cutlistItemDStatusMessage="delivered">112799</cutListItemId>
<bmarkerId>8f16ff80-1269-11ed-95e6-5254000e8e1a</bmarkerId>
<videoId>2912799</videoId>
<counter>1</counter>
<frameInSpecified>true</frameInSpecified>
<frameIn>15638</frameIn>
<frameOutSpecified>true</frameOutSpecified>
<frameOut>20082</frameOut>
<tcIn>00:10:25:13</tcIn>
<tcOut>00:13:23:07</tcOut>
<duration>177760</duration>
<BroadcastDate>2021-07-24</BroadcastDate>
<eventDate>2021-07-24</eventDate>
<resolutionFacet>HD</resolutionFacet>
<provider>DBC</provider>
<technicalrightholders>RH_DBC</technicalrightholders>
<rights>DBC</rights>
<materialType>DP</materialType>
<targetFilename>kral_schalke_trenink</targetFilename>
</cutlistItems>
</properties>
</EDLXML>
I got the right value from query if i do:
SELECT ExtractValue((SELECT job_xml from cutlist where job_xml is not null LIMIT 1), '//deliveredNum');
But when i change the limit amount i get back: Subquery return more than one row.
extractvalue expects two string arguments. When your subquery returns more than one row, you are not simply passing a string as the first argument (you are passing a set of results).
Instead of calling extractvalue once for your entire query, call it once for every row, like:
SELECT ExtractValue(job_xml, '//deliveredNum')
FROM cutlist
WHERE job_xml IS NOT NULL

Get value from JSON object having special character in key using SQL query

I have an outlet_details table having two columns(id and extended_attributes as a JSON object).
extended_attributes have values like
{
"parent-0-0-id": "DS-606",
"parent-0-1-id": "SD066",
"secondaryOutletCode": "MG_918"
}
I want to get parent-0-0-id's value, but when I'm trying to hit
SELECT extended_attributes->>'$.parent-0-0-id' AS 'parent00id' FROM outlet_details;
I'm getting an:
invalid JSON path expression error(3143).
You could just enclose the column name under quotes to separate out the name from escape characters.
SELECT extended_attributes->>"$.\"parent-0-0-id\"" AS 'parent00id' FROM outlet_details; should work

Pull data from JSON column and create new output with ABSENT ON NULL option

I have a JSON column in an Oracle DB where it was populated without the ABSENT ON NULL option and there are some pretty long lengths because of this.
I would like to trim things down and have created a new table similar to the first but I would like to select the JSON from form the old, add the ABSENT ON NULL option and place the new values in reducing the column length.
So I can see the JSON easy enough like
SELECT json_query(json_data,'$') FROM table;
This will give a result like:
{
"REC_TYPE_IND":"1",
"ID":"1234",
"OTHER_ID":"4321",
"LOCATION":null,
"EFF_BEG_DT":"19970101",
"EFF_END_DT":"99991231",
"NAME":"Joe",
"CITY":null
}
When I try to remove the null values like
SELECT json_object (json_query(json_data,'$') ABSENT ON NULL
RETURNING VARCHAR2(4000)
) AS col1 FROM table;
I get the following:
ORA-02000: missing VALUE keyword
I assume this is because the funcion json_object is expecting the format:
json_object ('REC_TYPE_IND' VALUE '1',
'ID' VALUE '1234')
Is there a way around this, to turn the JSON back into values that JSON_OBJECT can recognize like above or is there a function I am missing?

How to retrieve a data from inside a json postgres

In my database table I have a column named data and that column's type is jsonb. Here is a sample json of the column.
{"query": {"end-date": "2016-01-31", "start-date": "2016-01-01", "max-results": 1000, "start-index": 1 }}
This is the result in a formal format.
{
"query":{
"end-date":"2016-01-31",
"start-date":"2016-01-01",
"max-results":1000,
"start-index":1
}
}
I need to get the data from the 'start date' inside the 'query' element. How get the data from the start date from a pgsql query
You can use the Postgres in-built function named 'json_extract_path'. Document.
The first parameter in this function is the column name, the second parameter is JSON root element and the third parameter is the key name of which you want to get data.
select json_extract_path(data::json,'query','start-date') as test FROM "schema".tbl_name

how to order mysql result based on the json key of a json object stored in a column

jSonColoumn - data type (TEXT)
sample rows
{"scheduledTime":"2014-01-29 19:55:00"}
{"scheduledTime":"2014-01-29 22:55:00"}
{"scheduledTime":"2014-01-29 15:55:00"}
{"scheduledTime":"2014-01-29 08:55:00"}
i need to sort this result with the "scheduledTime" key of the json object stored in the column "jSonColoumn". (Simply i need to order by the result based on the "scheduledTime" key of the json)
Thanks
There are some MySQL JSON functions.
Like this:
select json_extract(columnX, '{"scheduledTime"}')