Let's say that we have the following table 'articles':
id_article | article_title
1 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
2 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
3 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
4 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
5 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
6 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
7 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"}
And let's say that we also have the following table 'seen':
id_seen | id_item_seen
1 | 3
2 | 5
3 | 5
4 | 3
5 | 3
6 | 3
7 | 3
8 | 2
9 | 2
10 | 4
11 | 1
12 | 7
13 | 7
I've executed the following SQL query:
select articles.id_article, articles.id_article, count(*) as seens from seen
inner join articles on articles.id_article = seen.id_item_seen
group by articles.id_article
order by seens desc
This will return the following result:
id_article | article_title | seens
1 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 1
2 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 2
3 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 5
4 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 1
5 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 2
6 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 0
7 | {"ar":"Arabic value", "en":"English value", "tr":"Turkish value"} | 2
This is ok, but I want to return not all of the stored JSON {}, but just a specified value of desired key (for example: "ar") as the following:
1 | Arabic value | 1
2 | Arabic value | 2
3 | Arabic value | 5
4 | Arabic value | 1
5 | Arabic value | 2
6 | Arabic value | 0
7 | Arabic value | 2
I know we could do this with PHP using json_decode() function, but How could we achieve this with SQL query?