php - Count Same Values On Mysql Table -
I want to count the maximum value on a table based on single field.
I have an image_count field on my desk; This means that different images do not show different thumbnails with the same image.
For example, I have an image named Stack.JPG and it has three thumbnails in different sizes.
DB with images with the same image.
Let's count the number in those files: 1.
When I upload another image again with three thumbnail sizes; Images uploaded and resized should have image_count: 2.
like this:
id | Thumbnail | Image | Counting | ----------------------------------------------- 1. Short | Stack_small.jpg | 1 | 2 | Medium | Stack_medium.jpg | 1 | 3 | Big | Stack_big.jpg | 1 | 4 | Short | Overflow_smul.jpg | 2 | 5 | Medium | Overflu_midim JPG | 2 | 6 | Big | Overflw_big.jpg | 2 | -----------------------------------------------
When I want to add a new image for DB, the count area should be "3". 7 no.
I think counting is to be a group, but I am using symfony2 with theory and when I try to get single SLAR, it is throwing exceptions.
My code:
Public function cgs images ($ entity_id, $ gallery_id) {$ em = $ this- & gt; GetEntityManager (); $ Qb = $ em- & gt; CreateQueryBuilder (); ('CSGalleryBundle: GalleryImage', 'i') - & gt; Select 'Left Goon' ('CSGalleryBundle: Gallery', 'g', 'with' $ count = $ qb - & gt; ('COUNT (i)'), 'G.id = i.gallery_id') - & gt; Where ($ qb-> expr () -> eq ('i.foreign_key', $ entity_id), $ qb- & gt; expr () - & gt; Eq ('g.id', $ gallery_id )) - & gt; GroupB ('i.image_count') - & gt; Setx Results (1) - & gt; GetQuery () - & gt; GetSingleScalarResult (); Return $ count + 1; }
Error:
What's the problem here? How can i solve Is there a better way of doing this?
Thank you!
You are right. You must use theory in order to increase performance. You can use this query:
$ count = $ qb - & gt; Select '(maximize i.image_count)' - & gt; ('CSGalleryBundle: GalleryImage', 'i' from) - & gt; Left Zone ('CSGalleryBundle: Gallery', 'g', 'with', 'g.id = i.gallery_id') - & gt; Where ($ qb-> expr () - & gt; eq ('i.e. foreign_key', $ entity_id), $ qb-> expr () - & gt; eq ('g.id', $ gallery_id )) - & gt; GetQuery () - & gt; GetSingleScalarResult (); Return $ count === empty? 1: $ count + 1;
Comments
Post a Comment