Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL get value from key in dictionary

I have a column in my database which is called user_log. It basically logs everything a user does on the page. I use MySQL.

It looks like that:

user_id user_log
1028 { "last_login":"2022-04-08 12:03:05", "blog_entry_at":"2022-04-08 12:43:12" }

Now, I want to extract all "last_login" and get the value of it.
It is a text field, but not a dict or something else

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use json_extract in MySQL.
SELECT user_id, json_extract(user_log, '$.last_login') as last_login FROM users;

Sample query: https://onecompiler.com/mysql/3y8a3brhr

More on json_extract here in the mariadb docs: https://mariadb.com/kb/en/json_extract/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading