简体   繁体   English

在Mysql表上计数相同的值

[英]Count Same Values On Mysql Table

I want to count the max value on the table based on single field. 我想基于单个字段计算表上的最大值。

I have a image_count field on my table; 我的桌子上有一个image_count字段; that's counts different images not different thumbnails with same image. 计算的是不同的图像,而不是具有相同图像的不同缩略图。

For example i have an image named stack.jpg and have three thumbnails in different sizes. 例如,我有一个名为stack.jpg的图像,并具有三个不同大小的缩略图。

So i have to save that resized images to db with same image_count. 因此,我必须将具有相同image_count的已调整大小的图像保存到db。

Let's give a count number to that files: 1. 让我们给这些文件计数:1。

When i upload another image with three thumbnail sizes again; 当我再次上传具有三个缩略图尺寸的另一张图片时; that uploaded and resized images must has the image_count: 2. 上传并调整大小的图像必须具有image_count:2。

So: 所以:

ID | Thumbnail |        Image         | count |
-----------------------------------------------
 1 |   Small   |   stack_small.jpg    |   1   |
 2 |   Medium  |   stack_medium.jpg   |   1   |
 3 |   Big     |   stack_big.jpg      |   1   |
 4 |   Small   |   overflw_small.jpg  |   2   |
 5 |   Medium  |   overflw_medium.jpg |   2   |
 6 |   Big     |   overflw_big.jpg    |   2   |
-----------------------------------------------

When i want to add a new image to db, count field has to be "3". 当我想向数据库添加新图像时,计数字段必须为“ 3”。 Not 7. 不是7。

I have to group by count i think but i'm using symfony2 with doctrine and when i try to get singleScalarResult with count, it is throwing exception. 我想按计数分组,但我在使用symfony2与教义,当我尝试使用count获得singleScalarResult时,它抛出异常。

My Code: 我的代码:

public function countImages($entity_id, $gallery_id)
{
    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder();

    $count = $qb
        ->select('COUNT(i)')
        ->from('CSGalleryBundle:GalleryImage', 'i')
        ->leftJoin('CSGalleryBundle:Gallery', 'g', 'WITH', 'g.id = i.gallery_id')
        ->where(
            $qb->expr()->eq('i.foreign_key', $entity_id),
            $qb->expr()->eq('g.id', $gallery_id)
        )
        ->groupBy('i.image_count')
        ->setMaxResults(1)
        ->getQuery()
        ->getSingleScalarResult();

    return $count + 1;
}

Error: 错误:

在此处输入图片说明

What is the problem here? 这里有什么问题? How can i solve? 我该如何解决? Is there any better way to do it? 有什么更好的方法吗?

Thank you! 谢谢!

You can change query with below for basic solve: 您可以使用以下命令更改查询以解决基本问题:

$count = $qb
   ->select('i')
   ->from('CSGalleryBundle:GalleryImage', 'i')
   ->leftJoin('CSGalleryBundle:Gallery', 'g', 'WITH', 'g.id = i.gallery_id')
   ->where(
       $qb->expr()->eq('i.foreign_key', $entity_id),
       $qb->expr()->eq('g.id', $gallery_id)
   )
   ->groupBy('i.image_count')
   ->getQuery()
   ->getResult();

return count($count);

Maybe you can review Doctrine Query Builder Documentation for more efficiently resolves. 也许您可以查看“ Doctrine查询生成器文档”以获得更有效的解决方案。

You are right. 你是对的。 You must use Doctrine way for increase performance. 您必须使用主义方式来提高性能。 You can use this query: 您可以使用以下查询:

$count = $qb
        ->select('MAX(i.image_count)')
        ->from('CSGalleryBundle:GalleryImage', 'i')
        ->leftJoin('CSGalleryBundle:Gallery', 'g', 'WITH', 'g.id = i.gallery_id')
        ->where(
            $qb->expr()->eq('i.foreign_key', $entity_id),
            $qb->expr()->eq('g.id', $gallery_id)
        )
        ->getQuery()
        ->getSingleScalarResult();

return $count === null ? 1 : $count + 1;

You can use count method with distinct 您可以将count方法与

$count = $qb
   ->select('COUNT(DISTINCT i)')
   ->from('CSGalleryBundle:GalleryImage', 'i')
   ->leftJoin('CSGalleryBundle:Gallery', 'g', 'WITH', 'g.id = i.gallery_id')
   ->where(
       $qb->expr()->eq('i.foreign_key', $entity_id),
       $qb->expr()->eq('g.id', $gallery_id)
   )
   ->groupBy('i.image_count')
   ->getQuery()
   ->getSingleScalarResult();

return $count;

I think it will throw an exception if there is no matched data. 我认为,如果没有匹配的数据,它将引发异常。

Just an attempt. 只是一个尝试。 I haven't check this. 我没有检查。 Why do you have leftJoin in your code? 为什么在代码中留下了Join?

$count = $qb
    ->select('MAX(i.count)')
    ->from('CSGalleryBundle:GalleryImage', 'i')
    ->getQuery()
    ->getSingleScalarResult();

return $count + 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM