计算行出现次数并打印它们
Counting row occurrences and printing them
一个 MySQL table 命名标签包含 2 列:标签和 video_id,行数超过 10 万。
其中 None 是主键,这意味着 tags 列中的值可能会以不同的 video_id 出现几次,例如:
column names: |tags|video_id|
values: tag1 video1
tag1 video2
tag2 video4
tag2 video5
tag2 video6
如何统计每个标签出现的次数并打印出来?类似于:
tag1 (2 Videos)
tag2 (3 Videos)
我的网站基于 CakePHP-3,我从 table 获取行的方式是这样的:
$query = $this->Tags->find('all');
$tags = $this->paginate($query);
变量标签现在有了结果,这就是我打印它们的方式:
foreach ($tags as $tag):
echo $tag->tags."<br>";
endforeach;
您可以利用基本的 SQL 逻辑来做到这一点,例如计数 video_id
(或 *
,这在某些情况下可能会给您带来更好的性能)和分组依据tags
.
$query = $this->Tags->find();
$query = $query
->select([
'tags',
'count' => $query->func()->count('video_id')
])
->group('tags');
$tags = $this->paginate($query);
这将创建类似于以下内容的查询:
SELECT tags, COUNT(video_id) as count
FROM tags
GROUP BY tags
生成的实体将包含 count
属性,您可以像访问任何其他 属性 一样访问它。这是一个小例子,另外使用复数感知翻译功能(当然不是必需的,但可能有用):
foreach ($tags as $tag):
echo $tag->tags . ' (' .
__n('{0} Video', '{0} Videos', $tag->count, $tag->count) .
')<br>';
endforeach;
另见
- Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
- Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having
- Cookbook > Internationalization & Localization > Plurals
- MySQL Manual > Tutorial / Creating and Using a Database / Retrieving Information from a Table / Counting Rows
您可以使用 cakephp count()
功能和 Group
选项
$query = $this->Tags->find('all');
$tags = $query->select([
'tags',
'count' => $query->func()->count('*')
])
->group('tags');
你可以通过
得到结果
foreach ($tags as $tag):
echo $tag->tags."(".$tag->count." Videos)";
endforeach;
一个 MySQL table 命名标签包含 2 列:标签和 video_id,行数超过 10 万。 其中 None 是主键,这意味着 tags 列中的值可能会以不同的 video_id 出现几次,例如:
column names: |tags|video_id|
values: tag1 video1
tag1 video2
tag2 video4
tag2 video5
tag2 video6
如何统计每个标签出现的次数并打印出来?类似于:
tag1 (2 Videos)
tag2 (3 Videos)
我的网站基于 CakePHP-3,我从 table 获取行的方式是这样的:
$query = $this->Tags->find('all');
$tags = $this->paginate($query);
变量标签现在有了结果,这就是我打印它们的方式:
foreach ($tags as $tag):
echo $tag->tags."<br>";
endforeach;
您可以利用基本的 SQL 逻辑来做到这一点,例如计数 video_id
(或 *
,这在某些情况下可能会给您带来更好的性能)和分组依据tags
.
$query = $this->Tags->find();
$query = $query
->select([
'tags',
'count' => $query->func()->count('video_id')
])
->group('tags');
$tags = $this->paginate($query);
这将创建类似于以下内容的查询:
SELECT tags, COUNT(video_id) as count
FROM tags
GROUP BY tags
生成的实体将包含 count
属性,您可以像访问任何其他 属性 一样访问它。这是一个小例子,另外使用复数感知翻译功能(当然不是必需的,但可能有用):
foreach ($tags as $tag):
echo $tag->tags . ' (' .
__n('{0} Video', '{0} Videos', $tag->count, $tag->count) .
')<br>';
endforeach;
另见
- Cookbook > Database Access & ORM > Query Builder > Using SQL Functions
- Cookbook > Database Access & ORM > Query Builder > Aggregates - Group and Having
- Cookbook > Internationalization & Localization > Plurals
- MySQL Manual > Tutorial / Creating and Using a Database / Retrieving Information from a Table / Counting Rows
您可以使用 cakephp count()
功能和 Group
选项
$query = $this->Tags->find('all');
$tags = $query->select([
'tags',
'count' => $query->func()->count('*')
])
->group('tags');
你可以通过
得到结果foreach ($tags as $tag):
echo $tag->tags."(".$tag->count." Videos)";
endforeach;