在Mysql表上计算相同的值

时间:2014-08-01 07:45:23

标签: php mysql symfony count

我想根据单个字段计算表格的最大值。

我的桌子上有一个image_count字段;这会计算不同的图像,而不是具有相同图像的不同缩略图。

例如,我有一个名为stack.jpg的图像,并且有三个不同大小的缩略图。

所以我必须将调整后的图像保存到具有相同image_count的数据库。

让我们为这些文件提供一个计数:1。

当我再次上传另外三个缩略图尺寸的图像时;上传和调整大小的图片必须具有image_count:2。

所以:

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   |
-----------------------------------------------

当我想向db添加新图像时,count字段必须是" 3"。不是7。

我必须按计数进行分组我认为但是我使用symfony2和doctrine,当我尝试使用count来获取singleScalarResult时,它会抛出异常。

我的代码:

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;
}

错误:

enter image description here

这是什么问题?我怎么解决?有没有更好的方法呢?

谢谢!

4 个答案:

答案 0 :(得分:1)

您可以使用以下更改查询进行基本求解:

$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);

也许您可以查看Doctrine Query Builder文档以更有效地解析。

答案 1 :(得分:1)

你是对的。您必须使用Doctrine方式来提高性能。您可以使用此查询:

$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;

答案 2 :(得分:1)

您可以将count方法与distinct

一起使用
$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;

我认为如果没有匹配的数据,它会抛出异常。

答案 3 :(得分:0)

只是一次尝试。我没有检查过这个。为什么你的代码中有leftEoin?

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

return $count + 1;