MariaDB 10.1 JsonGet_string - mysql

In one of our columns we store this example json string:
[{"Name":"Pay Amount","Value":"0.00"},{"Name":"Period","Value":"3"},{"Name":"Client","Value":"TestClient"},{"Name":"Our Reference","Value":""},{"Name":"Pay Type","Value":"Test"}]
We repeat the Names through out and the values will differ.
I've tried querying this data using JsonGet_string :
SELECT
JSONGET_string(Header, "Name") Name
FROM tbl
but what it does it selects the first one i.e PayAmount and it only displays a list of payamount it doesn't select anything for Period, Client etc.
The result that it returns looks like this:
| Name |
|----------|
| |
| PayAmount|
| PayAmount|
And it should return this:
| Name |
|-------------|
| |
| PayAmount |
| Period |
| Client |
| OutReference|
| Pay Type |
Any ideas?

Related

How to display the current recent rows according to time?

The following database generated by (select * from users).How can i display the recent rows according to time(i.e for time i had used date +%s).But here it displays all timing .I nee dthe rows with the recent update time.
| time | userid | groupid |
+------------+----------+-----------+
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
| 1476868065 | dnehra | HCG |
| 1476868065 | dprabhu | PDSP_DES |
My expected Output:(but it sould be generated by using some linux commands like date +%s).Is there any linux commands which fetches the only the last recent time rows (or) Is it possible to use inside insert mysql query by storing in some variable name.
| time | userid | groupid |
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
Do you want to represent the time in proper date formant? if so;
select from_unixtime(time), * from user order by time desc
You could specify that you only want items where the time is equal to that of the largest value that column holds in the table.
SELECT * FROM users where time = (Select max(time) from users);

grails - findBy highest id AND another criteria

I've looked a bunch of answers to this question here on SO and elsewhere but all I can track down is cases where people just want to find the highest id, the max dateCreated or the latest db entry but what I want to do is retrieve the latest object created that also matches another criteria. My domain class has the following properties: id, number, company, type, dateCreated and content. The company property can only be set to 'OYG' or 'BAW' and the number property is an auto incrementing int. What I want to do is retrieve the record with the highest number that also has its company property set to 'OYG' or 'BAW`.
So here's an example:
+----------------------------------------------------------+
| id | number | company | type | dateCreated | content |
+----------------------------------------------------------+
| 1 | 0 | OYG | TsAndCs | 15/09/2016 | stuff |
| 2 | 0 | BAW | TsAndCs | 15/09/2016 | stuff |
| 3 | 1 | OYG | TsAndCs | 16/09/2016 | stuff |
| 4 | 2 | OYG | TsAndCs | 17/09/2016 | stuff |
| 5 | 1 | BAW | TsAndCs | 16/09/2016 | stuff |
+----------------------------------------------------------+
I want to say def doc = Document.findByHighestNumberAndCompany('OYG') then it should bring back the object with id 4. def doc = Document.findByHighestNumberAndCompany('BAW') should bring back id 5's object, etc.
Any help would be appreciated. Thanks!
Despite Joshua Moore gave you a good solution, there is another simplier in one line.
MyDomain.findAllByCompany(company, [sort: 'number', order: 'desc', limit: 1])?.first()
Should be easy enough if you order by the number in descending order, and limit your results to one. So perhaps something like this?
String companyName = 'OYG'
def results = MyDomain.createCriteria().list() {
eq("company", companyName)
maxResults(1)
order("number", "desc")
}
println results[0].id // will print 4
Using this approach you could create a named query so you can pass the company name as a parameter.

export mysql table with data spread over multiple rows to csv

I have a mysql table which is filled with inputs from a webform on my website. The form has fields for last name, surname, email, phone, address, etc.... and when a user submits the form these data are stored in a mysql table in a rather strange way.
my table looks like this:
subission# | value | field | tstamp | and |many |more |columns
=====================================================================================
1 |john#server.com |email |1448898875 | | | |
1 |john |firstname|1448898875 | | | |
1 |doe |lastname |1448898875 | | | |
1 |london |city |1448898875 | | | |
2 |jane#aol.com |email |1448898870 | | | |
2 |jane |firstname|1448898870 | | | |
2 |doe |lastname |1448898870 | | | |
2 |new york |city |1448898870 | | | |
3 |tim #aol.com |email |1448838571 | | | |
3 |tim |firstname|1448838571 | | | |
3 |smith |lastname |1448838571 | | | |
3 |paris |city |1448838571 | | | |
I need to export these data to a csv file in order to import it to a newsletter script on some other server, but the server expects these data in a different format:
submission#,email,firstname,lastname,tstamp,.....
1,john#server.com,john,doe,london,1448898875,,,,
2,jane#aol.com,jane,doe,1448898870,,,,
The export as csv is not the problem, but how do I get all the data of one submission# into one row? Can anyone please point me into the right direction, how to accomplish this with SQL?
You can achieve the desired output, if you concatenate the field contents into a single field using concat() and group_concat() functions, where the values are separated by comma.
The only issue can be if for a particular submission any of the properies is missing. If that's the case, then you will need a helper table which lists all properies and you need to left join on that table. Since this is not the case for your sample data, I'm not providing the code for this scenario.
select concat(submission, ',', group_concat(`value` order by `field` asc), ',',tstamp)
from table group by submission, tstamp
If you need the field names in the 1st row, then create a separate query that conatenates the field names separated by commas and combine the 2 with union.

Select a record n times which n = times of occurrences

I want to select a record n times in which n is the number of times a string has occurred in a field.
Example:
mytable:
+--------+------------------------------------+
| id | content |
+--------+------------------------------------+
| 1 | This string contains two strings. |
| 2 | This is a string. |
| 3 | This does not contain our keyword. |
+--------+------------------------------------+
Now I want the result of such a hypothetical query to be like the following result:
/* hypothetical: this won't yield the desired result obviously */
SELECT * FROM mytable WHERE content LIKE "%string%";
+--------+------------------------------------+
| id | content |
+--------+------------------------------------+
| 1 | This string contains two strings. |
| 1 | This string contains two strings. |
| 2 | This is a string. |
+--------+------------------------------------+
Is this even possible?
Thanks

Mysql remove duplicate from a row and update the row

I have a mysql table like
+-----------------+---------------------------------------------------------------+---------+
| col_key |member_column | weight |
+-----------------+---------------------------------------------------------------+---------+
| 4:20131205:0922 | 018210020504;4.1672|018210020504;4.1672 | 8.3344 |
| 4:20131204:0923 | 015819070006;13.8584|015819070006;13.8584 | 27.7168 |
| 4:20131202:0922 | 018710040303;8.7864 | 8.7864 |
| 4:20131204:0923 | 017319010003;2.7044|017319010004;2.7044 | 5.4088 |
| 4:20131202:0922 | 055320020104;7.3357 | 7.3357 |
| 4:20131217:0922 | 019120020404;7.8727|019120020404;7.8727 | 15.7454 |
| 4:20131223:0923 | 011820010203;11.5213 | 11.5213 |
| 4:20131216:0925 | 018320010403;13.7416 | 13.7416 |
| 4:20131217:0922 | 017420020205;6.7384 | 6.7384 |
| 4:20131217:0922 | 019723010104;4.3660|050122010004;12.1407|050122010003;12.1407 | 28.6474 |
| 4:20131224:0926 | 022923040107;10.2461|022923040106;10.2461 | 20.4922 |
| 4:20131216:0925 | 050122010004;12.1407|050122010003;12.1407 | 24.2814 |
| 4:20131216:0925 | 061020030007;3.8048 | 3.8048 |
+-----------------+---------------------------------------------------------------+---------+
Here member_columns has values different member:weight which is | separated. Weight columns has total weight for the member in a particular row.
We need to remove duplicate members from the member_columns and update the weight correspondingly.
Example:
row with col_key 4:20131205:0922 has member_column in which members are repeated .
I need this columns updated to
4:20131205:0922 | 018210020504;4.1672 | 4.1672
Similarly for the column
4:20131217:0922 | 019723010104;4.3660|050122010004;12.1407|050122010003;12.1407|28.6474
I need it to be updated as
4:20131217:0922 | 019723010104;4.3660|050122010004;12.1407|16.5067
I was looking towards cursor for the solution but learn that cursor do not update the actual data.
Please HELP .
For this approach, you will need to take help from any server side script. So the Process to follow would be
1) select all rows having member_column as your resultset field from the table narrowing down by any condition of your choice
2) Iterate the result set
3) Explode the member_column field by "|" and form an array of strings. which would come as
array("4:20131217:0922", "019723010104;4.3660","050122010004;12.1407","16.5067");
4) Do all your computation and remove duplicates
5) once you have a unique array, implode it back with dash "|"
6) update the row back.
Hope it helps :)