Mysql order by percent of two values sum - mysql

So I need to order by percent counted from two values of one field where data is stored in JSON.
The field name where values are stored named program_invested_details and for example value is:
{"invested":"120.00","received":"1.08"}
I need that $query would be (received * 100 / invested) from that field
SELECT *, ($query) AS PERRCENT_TOTAL
FROM programs_list
WHERE program_add_status = 4 AND program_status = 1 ORDER BY PERRCENT_TOTAL DESC
how does it possible to make?

By default MySQL does not have any ability to parse a JSON string.
One option would be to use an extension such as common_schema which would add the ability to parse JSON and extract fields (see get_option). I am not sure of the performance hit you would take with this extension.
Another option would be to query all the data and parse the JSON in your client program. Once again, there would be significant performance impact if there is a lot of data.

Related

how to select rows where a specific string column matches at least one value inside a json array?

SELECT name FROM accounts WHERE Name in ("name1","name2");
the values are being sent inside a json array
["name1","name2"]
currently i just convert the array into json string and remove the first and last characters
"name1","name2"
but could i just keep the array intact? i tried json_contains
SELECT name FROM accounts WHERE JSON_CONTAINS(name,'["name1","name2"]');
my understanding as to which why that didn't work is because name column isn't json string array
Core issue is you can't "prepare" flex statements without some sort of preprocessing.
Generally you'll want to do input validation etc, on the application side regardless, and then use some sort of pre-constructor if you're not using an ORM.
ie:
$values = ["name1", "name2"];
// Validation should happen here
$inputs = substr(str_repeat("?,", count($values)), 0, -1);
$bind = str_repeat("s", count($values));
$sqli = "SELECT name FROM accounts WHERE Name in ($inputs);";
...
$stmt->bind_param($bind, ...$values);
...
You can use the same principal for PDO as well, but regaredless you're gunna want to handle the validation layer on the application side, and there is no "easy" way to inject "IN" statements into prepared SQL.

Mysql JSON Multi-dimensional Array

which form should have json code in mysql field ?
I need users datas (user_id is the key) with 3 values (3 informations : name , age, sex)
145:"name,age,sex",
148:"name,age,sex",
200:"name,age,sex"
I am using mysql version 5.6 and the datas will be inserted with SQL code
is it correct to store it in that way in mysql to retrieve with php and json_decode?
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
thank you
Do you ever want to be able to write a query like select * from users where sex=1? If so, don't store the JSON as text. Store each value (id, name, age, sex) in a column of its own.
Even if you do want to store the JSON as a string, it would probably be better organised like this:
[
{"id":236,"name":"paul","age":26,"sex":1},
{"id":2515,"name":"fred","age":42,"sex":1},
{"id":2516:"jane","age":21,"sex":0}
]
You would need to manipulate it a bit after querying, but you would have more meaningful data.
But if all you want is to store that text, so you can retrieve it as text later, then what you have is fine.
with datas in this form
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
and json_decode($myfield);
I get NULL
if I echo $myfield I get exatly what is stored in the database
what is wrong with N
[{236:"paul,26,1"},{2515:"fred,42,1"},{2515:"jane,21,0"}]
If I parse it here http://json.parser.online.fr/
I get Syntax error

MySQL full text search on JSON data

I'm trying to replicate the following LIKE query using a full text search on JSON data;
SELECT * FROM table
WHERE response LIKE '%"prod_id": "foo"%'
AND response LIKE '%"start_date": "2016-07-13"%'
In my database the above query returns 28 rows
This is my attempt:
SELECT * FROM table
WHERE MATCH(response)
AGAINST('+"\"prod_id\": \"foo\"",+"\"start_date\": \"2016-07-13\""')
However this returns over 4,500 rows (the same as running the first query for only the prod_id ~1,900 rows when running the first query on just the date)
It was my understanding that +"text here" would indicate a required word, and that literal double quotes (present in the JSON data) should be escaped, and that , would indicate a split between the two strings I'm looking for. What am I not understanding correctly? Is there any point in running this as a full text query anyway?
Thanks to #Sevle I've tweaked my query like so, and it's returning the correct results;
SELECT * FROM table
WHERE MATCH(response)
AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"' IN BOOLEAN MODE)
The comma was not helping and I was escaping the wrong characters, and of course I did need IN BOOLEAN MODE to be added. Finally, I removed the double quotes I was searching for in the JSON string.
It may also be worth noting that as I'm using PHP PDO to run this query I also had to make the following tweaks.
Instead of constructing the query like so trying to bind the variables like I normally would;
$query = $db->prepare('...AGAINST('+\"prod_id: :prod_id\" +\"start_date: :start_date\"');
$query->execute(array('prod_id' => 'foo', 'start_date' => '2016-07-13'));
I had to do this, as I found I could not bind variables in full text searches
$sql_against = $db->quote('...AGAINST('+\"prod_id: foo\" +\"start_date: 2016-07-13\"');
$query = $db->prepare("...AGAINST($sql_against IN BOOLEAN MODE)")

Is there any way to get value from JSON format through mysql query or function/procedure?

Is there any way to get value from JSON format which are stored in database field.
I had tried like below but sometimes it return wrong value and it doesn't seems to correct thing to fetch records.
Actually I am trying to addition of all sizes
SELECT
substring_index(substr(size,locate('"xs":"',size)+char_length('"xs":"')),'"',1)+
substring_index(substr(size,locate('"s":"',size)+char_length('"s":"')),'"',1)+
substring_index(substr(size,locate('"m":"',size)+char_length('"m":"')),'"',1)+
substring_index(substr(size,locate('"l":"',size)+char_length('"l":"')),'"',1)+
substring_index(substr(size,locate('"xl":"',size)+char_length('"xl":"')),'"',1)+
substring_index(substr(size,locate('"xxl":"',size)+char_length('"xxl":"')),'"',1)+ substring_index(substr(size,locate('"xxxl":"',size)+char_length('"xxxl":"')),'"',1)+
substring_index(substr(size,locate('"xs\/s":"',size)+char_length('"xs\/s":"')),'"',1)+ substring_index(substr(size,locate('"m\\/l":"',size)+char_length('"m\\/l":"')),'"',1)+
substring_index(substr(size,locate('"xl\\\/xxl":"',size)+char_length('"xl\\\/xxl":"')),'"',1)
as qty FROM `table_name`
I have also taken the reference to catch numeric value from a string from below question of StackOverflow:
How do you extract a numerical value from a string in a MySQL query?
it returns correct value and i can do addition addition of return value,,but for 2 digit number it is not good for me (like if it return 15 so it is 6 for my case)

passing a variable to SQL statement in KNIME

Using KNIME, I would like to analyze data in a specific subset of columns in my database
but without using limiting SQL queries such as
Select *
From table
Where name like 'PAIN%'
Is there a way to do this in KNIME?
Try to find specific value within the column of choice by using:
Select distinct(column_name) from table;
You can pick from the expected result to filter your data
Select * from table column_name like 'result_one';
Assuming the column_name data type is in character.
To filer columns use the "Column Filter" node. You can filter the columns specifically, by RegEx on the column name or by column type (int, double, etc.) To filter rows based on content, use the "Row Filter" node, and select column to test and "filter based on collection elements" using pattern matching. This can also use RegEx. For mulitple columns use multiple nodes.
the knime did not support like for now, so I used the mysql locate or FIND_IN_SET function
SELECT id FROM address where LOCATE($street_Arr[0]$,street) > 0
SELECT id FROM address where FIND_IN_SET($street_Arr[0]$,street) > 0
however in the same situation u might be able to use knime joins much faster.