Skip to content

Instantly share code, notes, and snippets.

@andergmartins
Created January 8, 2015 15:11
Show Gist options
  • Save andergmartins/184fa7303a413a95249d to your computer and use it in GitHub Desktop.
Save andergmartins/184fa7303a413a95249d to your computer and use it in GitHub Desktop.
Extract data from JSON string inside a MySQL field. In this case, extract metadesc and page title from the menu params
SELECT m.id, m.title,
@tmp_search := '"menu-meta_description":"' AS tmp_search,
@tmp_search_len := LENGTH(@tmp_search) AS tmp_search_len,
@start := LOCATE(@tmp_search, m.params) AS tmp_start,
@end := LOCATE('"', m.params, @start + @tmp_search_len) AS tmp_end,
SUBSTRING(m.params, @start + @tmp_search_len, @end - @start - @tmp_search_len) AS metadesc,
@tmp_search := '"page_title":"' AS tmp_search,
@tmp_search_len := LENGTH(@tmp_search) AS tmp_search_len,
@start := LOCATE(@tmp_search, m.params) AS tmp_start,
@end := LOCATE('"', m.params, @start + @tmp_search_len) AS tmp_end,
SUBSTRING(m.params, @start + @tmp_search_len, @end - @start - @tmp_search_len) AS metatitle
FROM j_menu AS m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment