I want to get concatenated list of values from serialized string using MySQL query.The sample string is
a:4:{s:11:"customersId";i:381032;s:12:"commerceName";s:9:"amazonmws";s:9:"serviceId";i:32042;s:9:"trackings";a:2:{i:0;a:10:{s:12:"customers_id";i:381032;s:13:"commerce_name";s:9:"amazonmws";s:10:"service_id";i:32042;s:15:"commerce_ref_id";s:19:"112-1414980-3467405";s:12:"order_number";i:151434392;s:11:"tracking_no";s:15:"995365613134160";s:7:"carrier";s:3:"FDX";s:8:"order_id";i:151434392;s:7:"details";a:1:{i:0;a:2:{s:4:"wsku";s:12:"KZW4OC-F0015";s:3:"qty";s:9:"000000001";}}s:20:"commerce_account_key";N;}i:1;a:10:{s:12:"customers_id";i:381032;s:13:"commerce_name";s:9:"amazonmws";s:10:"service_id";i:32042;s:15:"commerce_ref_id";s:19:"002-5809777-9709040";s:12:"order_number";i:151536332;s:11:"tracking_no";s:15:"995365613134153";s:7:"carrier";s:3:"FDX";s:8:"order_id";i:151536332;s:7:"details";a:1:{i:0;a:2:{s:4:"wsku";s:12:"KZW4OC-F0011";s:3:"qty";s:9:"000000001";}}s:20:"commerce_account_key";N;}}}
I want the list of values next to key order_id in above string.
I have tried with substring_index.
Please find my sample query
select SUBSTRING_INDEX(substring_index('serialized_sting','"order_id";i:',-1),';s:7:',1)
I get only one value from string,but not all
Related
There is a column in one of my tables which contains the value like
{"firstname":"aaa","secondname:"bbb","lastname":"ccc","age":"18","Address:"xxxxxxxxxxxxxx"}
I am in need of SQL query to extract the value secondname (here the value will be bbb)
and I also need a update query to set the second value as empty
the output will be like
{"firstname":"aaa","secondname:"","lastname":"ccc","age":"18","Address:"xxxxxxxxxxxxxx"}
I want to update my columns for rows specified by WHERE command, but I want to update my field in a way that it extracts number part of the string from each specified field, multiplies that with a number (that I will specify) and give number output in all those specific fields extracted by WHERE command in that column.
For example, assume I want to update all my fields in a column which are like (5.6 AUD/1000, 4.5 AUD/1000, 9.7 AUD/1000), so I want to first identify fields ending with /1000 and update only those fields in the column by multiplying the number part of the string (which is 5.6, 4.5, 9.7) with any number (let's say 10). I want that other fields on the column remains unchanged.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
gives me all the specific fields that I wish to update. But I want to update them in the way I specified above, which is that I want to extract number part from the string and multiply that with a number and update those fields only.
I am able to extract all the fields with the condition I mentioned, I'm facing difficulty in update these fields in the column.
SELECT * from sorted WHERE Column8 REGEXP '/1000$';
SELECT CAST(Column8 AS UNSIGNED)*10 FROM sorted
wHERE
column8 REGEXP '/1000$';
The above code gives me required updated fields, but I want them reflected in my column.
I expect my output to be a column where only those fields ending with '/1000' should get updated in a way that the number part of the string is multiplied with 10.
I have casted the varchar field named string to decimal type and multiplied with static value 10 . I have checked in sql server.
DECLARE #temp TABLE
(
string NVARCHAR(50)
)
INSERT INTO #temp (string)
VALUES
('5.6 AUD/1000'),
('4.5 AUD/1000'),
('9.7 AUD/1000')
select cast(left(string, patindex('%[^0-9./]%', string) - 1) As decimal(18,2))* 10
from #temp
I have an array of string say it is ["a1","a2","a3"] and want to pass this array into the query.
The purpose is not to select the elements from database that are in this array or not in this, but is to select the elements in this array that are not inside the database.
Assume there is a table that is
create table table1(
id int primary key not null,
name text
);
in side the table1, it may have
[1,"a1"],[2,"a2"],[3, "b3"],[11,"str2"],[14,"california"],[15,"US"],[16,"aus"]
when I pass the array ["a1","a2","a3"] into the query, it could find that element "a3" is not inside table1 as "a1" and "a2" can be found, but "a3" cannot.
I have thought a possible solution to write in sql but it seems I cannot write out the query
My thought is
select [something] from [an array](/*this is something I passed from java or golang*/)
where [something] not in (select column from a table)
My thought is that select column from a table is a collection and an array is also a collection, but when writing into query, I stuck at there
I just wonder whether it is possible that I could organise the query and leave the place as a "?" and if I pass that array into ?, it will find the elements that are inside the array, but not inside the table.
Your best course of action here is to use a Table function like this one:
T-SQL split string
Then pass your array to your query in the form of a coma separated string.
For example:
SELECT name FROM dbo.mySplitFunction('a1,a2,13') WHERE name NOT IN (SELECT DISTINCT myField FROM myTable)
I need to get all the unique key values from all the rows. Each row has different keys and values Please find the above image of the column.
eg: one row looks like
{"START_TIME":1549002807568,"PARSING.QUERY_FORMED":1549002807586,"CUBES_WITH_PERMISSIONS":1549002807568,"PARSING.CUBE_MATCH_SELECTED":1549002807586,"POTENTIAL_COMPLETIONS_ADDED":1549002807587,"QUERY_PARSED":1549002807586,"SUGGESTIONS_FORMED":1549002807606,"PARSING.SEQUENCES_GENERATED":1549002807568,"PARSING.NGRAM_MATCHES_CACHED":1549002807585}
Tested this with two rows of data, all key_value pairs are identical except in second JSON there is one additional NEW_KEY and PARSING.NGRAM_MATCHES_CACHED values are different.
with data as
(
select stack(2, --data example
'{"START_TIME":1549002807568,"PARSING.QUERY_FORMED":1549002807586,"CUBES_WITH_PERMISSIONS":1549002807568,"PARSING.CUBE_MATCH_SELECTED":1549002807586,"POTENTIAL_COMPLETIONS_ADDED":1549002807587,"QUERY_PARSED":1549002807586,"SUGGESTIONS_FORMED":1549002807606,"PARSING.SEQUENCES_GENERATED":1549002807568,"PARSING.NGRAM_MATCHES_CACHED":1549002807585}',
'{"NEW_KEY":12345,"START_TIME":1549002807568,"PARSING.QUERY_FORMED":1549002807586,"CUBES_WITH_PERMISSIONS":1549002807568,"PARSING.CUBE_MATCH_SELECTED":1549002807586,"POTENTIAL_COMPLETIONS_ADDED":1549002807587,"QUERY_PARSED":1549002807586,"SUGGESTIONS_FORMED":1549002807606,"PARSING.SEQUENCES_GENERATED":1549002807568,"PARSING.NGRAM_MATCHES_CACHED":154900280758}'
) as str
)
select str_to_map(concat_ws(',',collect_set(key_value)),',',':') --collect set, concatenate and convert to map
from
(
select explode(split(regexp_replace (str,'[{}"]',''),',')) key_value from data --remove JSON delimiters, split and explode pairs
)s;
Result:
OK
{"START_TIME":"1549002807568","PARSING.QUERY_FORMED":"1549002807586","CUBES_WITH_PERMISSIONS":"1549002807568","PARSING.CUBE_MATCH_SELECTED":"1549002807586","POTENTIAL_COMPLETIONS_ADDED":"1549002807587","QUERY_PARSED":"1549002807586","SUGGESTIONS_FORMED":"1549002807606","PARSING.SEQUENCES_GENERATED":"1549002807568","PARSING.NGRAM_MATCHES_CACHED":"154900280758","NEW_KEY":"12345"}
Time taken: 158.414 seconds, Fetched: 1 row(s)
Of course, "PARSING.NGRAM_MATCHES_CACHED" exists only one time in the result, because map does not allow the same key twice. All key_values are unique.
Read comments in the code please.
I'm having an Active Record in my rails application in which each row of the table MyTable contains:
stringArr --> an array of string
id
Now I want to query my database to pull all the records in which a particular queryString is present in strinArr of that record.
I know we can do MyTable.where(xyz:someXYZ) to pull all the records with xyz value as someXYZ?
But how can I do this query?
To fetch all record, which contains queryString in an array of records, you can do -
MyTable.where("column_name LIKE ?","'%queryString%'"})
This will do exact query string search means return all data, which column_name contains queryString.
You can use the sql like clause
MyTable.where("stringArr LIKE :keywords",{keywords: '%querystring%'})