I am trying to fetch the data from the database where the value is inserted in JSON format, I read the various threads available online but these all are not worked and match the query or bit tough to implement for me as a beginner.
Please help is this possible to fetch the data if the saving this formate in DB though MySQL query?
I am trying to do this from last 2 days but all functions and login was not worked.
Please help.
Is JSON.parse() even part of SQL? It’s not something I’ve tried, but as far as I can tell it’s a javascript function.
There are various JSON functions for the types of SQL
Though it begs the question: why store the data this way? It may not have been your choice, but one value per column makes life a lot easier.
How did this json get produced? It is a json array containing a json object, requiring you to reference the zero’th array element and the in_dt property.
json_extract returns a json quoted value. all the attempts using date_format on that value should have been producing warnings about incorrect datetime values.
To get this to ‘work’ you must use json_unquote -
SELECT *
FROM `table`
WHERE json_unquote(json_extract(log_in,'$[0].in_dt')) BETWEEN '2020-01-01' AND '2020-01-05';
Given that none of this can use an index, and you must perform operations on it in every query to use the values, you need to do what @SamA74 has already recommended and just store this data in two separate columns.
Its working but can we use DATE_FORMAT like: CASE 1.DATE_FORMAT(json_unquote(json_extract(log_in,'$[0].in_dt')), '%Y-%m-%d')
but this is not working. this is the first query I never used before and it’s really very interesting for me to learn something new.
The values are already in the proper format. There’s no need to apply date_format() to them, unless you are not actually showing us the real values that are stored? Are they actually date time values?
Using this in the WHERE clause in the query that has been the subject in this thread works for me. If something doesn’t work for you, must post the entire query you are attempting and post what result you did get. Also, how are you executing these queries? Directly against the database using a tool like phpmyadmin or are you using php code?
If the information you have been showing is incomplete or adulterated, the solutions may not work because they don’t take into account all the extents and limits of the problem. Programming is an exact science.
Please help me this case last that what I will do if join with two different database.
like: SELECT * FROM db1.table a JOIN db2.table b ON b.col = JSON_UNQUOTE(JSON_EXTRACT(a.col,'$[0].in_dt'));
I am trying to explain how your code will work when I use two different database.
without JSON_UNQUOTE(JSON_EXTRACT()) I can use this documentation here but in this json case what I will do. and how I will join the two different database.