I want to create a column numberofimage
. In which each cell in the numberofimage
column will display the number of images in the addimage
column
format of each cell in addimage
column like:
[{"file":"vstorage.com/sohoa/han01//123.jpg","name":"123.jpg","type":"image/jpeg","size":239709},{"file":"storage.com/sohoa/han01//456.jpg","name":"456.png","type":"image/png","size":1030361}]
I would count the number of each cell like this, but wrong result:
SELECT * FROM `surveytemplate1`
WHERE addphoto LIKE '%.gif%'
OR addphoto LIKE '"Type:"image/jpeg"'
OR addphoto LIKE '"Type":"image/png"'
OR addphoto LIKE '"Type":"image/jpg"'
So in the example above there are 2 pictures because there are 2 words: "Type:"image/jpeg
" and "Type":"image/png
"
So what formula should I use to achieve my desired results?
2
Answers
A possible solution:
Explanation:
json_table
converts your JSON into a relation calledjt
from
whichselect
ing `count(*) yields your resultjson_table
specifies the columns, here I was only interested offile
Fiddle: https://www.db-fiddle.com/
To count images you can use
json_length(addphoto)
.Or expand json by JSON_TABLE and count rows.
See example:
Test source:
Simple query
Output
Example with
JSON_TABLE
If you whant count only images of selected types
You can extract only image type from json (
imagetype VARCHAR(100) PATH "$.type"
).fiddle