MP2-431 Invalid SQL built when counting media item

Fixes from review
Merge remote-tracking branch 'origin/FEAT_MP2-431_Invalid_SQL_counting_media_items' into MrTechno_dev