I have mysql 'text' column with data as json which looks like below, how to query using specific value, where Banking is model name , info is field name
Banking
info : text
info: {"age": 23, name: "John"}
In my opinion you can try search by LIKE query as below but because your database save data by text so it is very difficult to handle exception case. So I think you should try use json type of mysql or split it into table and save age as integer.
select * from bankings where info LIKE '%"age": 23%'
Ruby
Banking.where('infor LIKE ?', '%"age": 23%')
You should use ActiveRecord to perform this query using ->> operator:
Banking.where("info ->> 'name' = 'John'")
Related
Hope you all are doing well.
Today morning I was writing some sql query and I found a situation where I need your suggestion on that so here is the situation:
I've a table in mysql called users it has id column which is bigint
now when I'm trying to extract data with a query like :
select * from users where id = 123
in this case it'll show the result for user 123
now the situation here is if I run the same query like:
select * from users where id = 123b
now the issue here is it is still giving me the data for user 123
need your suggestion guys on the same, I did some R&D on the same but didn't found much usefull.
Thank you
Your question is tagged with mysqli which means you are using PHP to build your query. If you pass the string 123b as an integer, PHP will keep the leading digits:
var_dump((int)'123b');
# output: int(123)
MySQL then execute your query with this well formed integer 123.
If you execute a raw SQL query using any tool of your choice you should get an error like the following:
Unknown column '123b' in 'where clause'
This is because 123b cannot be an integer as it contains a letter, and isn't a string as it has no string delimiters.
Note that MySQL can still interpret strings without delimiters like hexadecimal values. For example, 0x31323362 is a valid string value for 123b and does not need quotes around it.
subcategory_id column data are in JSON format
like :
["3","4","5"]
Now I want to select all data from the table where subcategory_id = 3. How can I do this with Laravel query builder?
you can use this.
DB::table('table_name')->whereJsonContains('subcategory_id ','3')->get();
I had the same issue with whereJsonContains. Make sure you are inserting the data in a proper format, because you wouldn't get an error if you try to insert to JSON column like this:
YourTable::factory()->create([
'subcategory_id' => '3' // this should be like ['3']
]);
If the data is inserted properly, you can use whereJsonContains,
YourTable::whereJsonContains('subcategory_id', "3")->get();
Which returns all rows having "3" in subcategory_id array column.
This feature is not supported by the SQLite database
I've imported a raw data export from Unity into PostgreSQL using the JSON file that Unity offers.
Sample of data:
{"name":"EVENT1","ts":1534117312648,"userid":"1e77723b38980460ea307db5fca875fd","sessionid":"3188654687037448331","platform":"AndroidPlayer","sdk_ver":"u2017.4.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; LG-H820 Build/NRD90U)","submit_time":1534118874283,"custom_params":{"the Daily Bonus":"RewardGems"},"country":"US","city":"Fayetteville","appid":"50d97d88-096c-4a4b-8daa-390e239974f8","type":"custom"}
{"name":"GAME1","ts":1534107814910,"userid":"f029c3982539e4eeea171132bd9cf8c9","sessionid":"220388644753439310","platform":"AndroidPlayer","sdk_ver":"u2017.4.1f1","debug_device":false,"user_agent":"Dalvik/2.1.0 (Linux; U; Android 7.0; SM-G570M Build/NRD90M)","submit_time":1534118931705,"custom_params":{"Heavy Slam":"1","Flame Breath":"1","Cure":"1","Chop":"1","Bite":"7","Fang Fireball":"10","Axe Throw":"2"},"country":"BR","city":"Várzea Grande","appid":"50d97d88-096c-4a4b-8daa-390e239974f8","type":"custom"}
The JSON data is in the values column in the temp_json table as a text data type. When trying to CAST the column as a JSON datatype I get the following error:
ERROR: invalid input syntax for type json
DETAIL: Character with value 0x0a must be escaped.
CONTEXT: JSON data, line 1: ...tric Jacket":"1","Bolt":"5","Axe Throw":"1","Toss
SQL state: 22P02
As a result I've had to use to_json(values)to convert the text column into JSON. When I attempt to run the following query:
SELECT to_json(values) -> 'name'
FROM temp_json
My query results in NULL. I've searched around and found an answer that stated to try the following query:
select json_array_elements(to_json(values)) ->> 'name'
from temp_json
Although that results in the following error:
ERROR: cannot call json_array_elements on a scalar
SQL state: 22023
I'm extremely new to JSON and PostgreSQL so apologies for the noob question. Any help would be very much appreciated. I feel like this should be an easy thing to figure out, but I can't seem to find a solution.
If you are trying to return the "name" parameter from the JSON string you could use the JSON_EXTRACT_PATH_TEXT function. See docs:
https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html
Here is an example of how to use it assuming the JSON string is in the values column in the temp_json table:
SELECT JSON_EXTRACT_PATH_TEXT(values, 'name') AS name
FROM temp_json;
Hope this helps!
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
I want to save the ruby hash in the database like
Metrics table has name,values fields.
metrics.create("Registered", '{"Gender": "Male", "Age": 21}')
I want the query should run like this.
select count(*) from metrics where name=“Registered” and values.age > 20
As per best of my knowledge it will not work. But Is there any possibility to achieve this?
If the database used is postgresql, you can have a json column named values and then use json_extract_path_text(values, age) to extract 'age'
Reference for json_extract_path_text can be seen here