Php 对Mysql表中的相同值进行计数
我想根据单个字段计算表上的最大值 我的桌子上有一个图像计数字段;这是统计不同的图像,而不是相同图像的不同缩略图 例如,我有一个名为stack.jpg的图像,有三个大小不同的缩略图 因此,我必须以相同的图像计数将调整大小的图像保存到db 让我们给这些文件一个计数:1 当我再次上传另一张有三个缩略图的图片时;上载和调整大小的图像必须具有图像计数:2 因此: 当我想向数据库添加一个新图像时,count字段必须是“3”。不是7 我想我必须按计数分组,但我使用的是symfony2和doctrine,当我尝试使用count获得singleScalarResult时,它会抛出异常 我的代码:Php 对Mysql表中的相同值进行计数,php,mysql,symfony,count,Php,Mysql,Symfony,Count,我想根据单个字段计算表上的最大值 我的桌子上有一个图像计数字段;这是统计不同的图像,而不是相同图像的不同缩略图 例如,我有一个名为stack.jpg的图像,有三个大小不同的缩略图 因此,我必须以相同的图像计数将调整大小的图像保存到db 让我们给这些文件一个计数:1 当我再次上传另一张有三个缩略图的图片时;上载和调整大小的图像必须具有图像计数:2 因此: 当我想向数据库添加一个新图像时,count字段必须是“3”。不是7 我想我必须按计数分组,但我使用的是symfony2和doctrine,当我尝
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;
}
错误:
这里有什么问题?我怎样才能解决这个问题?有没有更好的办法
谢谢大家! 您可以使用以下命令更改查询以进行基本求解:
$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);
也许您可以查看条令查询生成器文档以获得更有效的解决方案。您可以使用以下更改查询以获得基本解决方案:
$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);
也许您可以查看条令查询生成器文档以获得更有效的解决方案。您可以使用以下更改查询以获得基本解决方案:
$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);
也许您可以查看条令查询生成器文档以获得更有效的解决方案。您可以使用以下更改查询以获得基本解决方案:
$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);
也许您可以查看条令查询生成器文档,以便更有效地解决问题。只是一个尝试。我还没有检查这个。为什么在代码中有leftJoin
$count = $qb
->select('MAX(i.count)')
->from('CSGalleryBundle:GalleryImage', 'i')
->getQuery()
->getSingleScalarResult();
return $count + 1;
只是一次尝试。我还没有检查这个。为什么在代码中有leftJoin
$count = $qb
->select('MAX(i.count)')
->from('CSGalleryBundle:GalleryImage', 'i')
->getQuery()
->getSingleScalarResult();
return $count + 1;
只是一次尝试。我还没有检查这个。为什么在代码中有leftJoin
$count = $qb
->select('MAX(i.count)')
->from('CSGalleryBundle:GalleryImage', 'i')
->getQuery()
->getSingleScalarResult();
return $count + 1;
只是一次尝试。我还没有检查这个。为什么在代码中有leftJoin
$count = $qb
->select('MAX(i.count)')
->from('CSGalleryBundle:GalleryImage', 'i')
->getQuery()
->getSingleScalarResult();
return $count + 1;
你说得对。您必须使用这种方法来提高性能。您可以使用以下查询:
$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;
你说得对。您必须使用这种方法来提高性能。您可以使用以下查询:
$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;
你说得对。您必须使用这种方法来提高性能。您可以使用以下查询:
$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;
你说得对。您必须使用这种方法来提高性能。您可以使用以下查询:
$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;
您可以将计数方法与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;
我认为如果没有匹配的数据,它将抛出异常。您可以使用带有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;
我认为如果没有匹配的数据,它将抛出异常。您可以使用带有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;
我认为如果没有匹配的数据,它将抛出异常。您可以使用带有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;
我想如果没有匹配的数据,它会抛出一个异常。Bro这在很多方面对我都不起作用。。。请再读一遍这个问题。那么你想完成什么呢?获取列“count”的下一个数字(我认为这个名称可能会引起误解),或者只获取图像的数量(其中图像可以有多个记录,但在“count”列中都具有相同的值)?尝试转储查询。兄弟,这在很多方面对我都不起作用。。。请再读一遍这个问题。那么你想完成什么呢?获取列“count”的下一个数字(我认为这个名称可能会引起误解),或者只获取图像的数量(其中图像可以有多个记录,但在“count”列中都具有相同的值)?尝试转储查询。兄弟,这在很多方面对我都不起作用。。。请再读一遍这个问题。那么你想完成什么呢?获取列“count”的下一个数字(我认为这个名称可能会引起误解),或者只获取图像的数量(其中图像可以有多个记录,但在“count”列中都具有相同的值)?尝试转储查询。兄弟,这在很多方面对我都不起作用。。。请再读一遍这个问题。那么你想完成什么呢?获取列“count”的下一个数字(我认为这个名称可能会引起误解),或者只获取图像的数量(其中图像可以有多个记录,但在“count”列中都具有相同的值)?试着抛开这个疑问。天哪,上帝保佑你!谢谢:)这就是我一直在寻找的方式!!!你就是那个人!上帝保佑你!谢谢:)这就是我一直在寻找的方式!!!你就是那个人!上帝保佑你!谢谢:)这就是我一直在寻找的方式!!!你就是那个人!上帝保佑你!谢谢:)这就是我一直在寻找的方式!!!你就是那个人!