[英]Retrieve all data from table, then count how many results there are in another
I am wondering if this is possible using just one MySQL query. 我想知道这是否可能只使用一个MySQL查询。
What I am making is a tagging system. 我正在制作的是标记系统。 Therefore, I have two tables:
因此,我有两个表:
Tags, id | heading
Tagged, id | image_id | tag_id | timestamp | strength
Everything works fine, but now I am getting to the stage where I am displaying the tags to my users and plan to sort them by how many times a tag has been used. 一切正常,但现在我正在进入我向用户显示标签的阶段,并计划根据标签的使用次数对它们进行排序。
So in a nutshell, I need something that will do the following, 所以简而言之,我需要做以下事情,
id = 34
. id = 34
。 Now I have tried looking into joins, left joins, etc., but it all looks very overwhelming and I am finding it hard to get a grasp of it. 现在我已经尝试过研究连接,左连接等等,但这一切看起来都非常庞大,我发现很难掌握它。
If anyone could explain how to do this easily, that would be great! 如果有人能够轻松解释如何做到这一点,那就太棒了!
EDIT 编辑
I currently have this 我现在有这个
SELECT tags.id, COUNT( tagged.id ) AS count
FROM tags
INNER JOIN tagged ON tags.id = tagged.tag_id
WHERE tagged.image_id = '1'
GROUP BY tags.id
ORDER BY count DESC
This only returns how many times a tag has been tagged to the current image_id, how would i make it so that it counts from all images? 这只返回标签被标记到当前image_id的次数,我将如何制作它以便从所有图像中计数?
You can use GROUP BY
to group together the tags that are the same, and use COUNT()
to count them: 您可以使用
GROUP BY
将相同的标记组合在一起,并使用COUNT()
对它们进行计数:
SELECT tags.id as id,
COUNT(1) as cnt
FROM tags INNER JOIN tagged
ON tags.id = tagged.tag_id
GROUP BY tags.id
ORDER BY cnt DESC
Here's what it does exactly: 这是它的确切作用:
COUNT
function counts how many elements are in each group. COUNT
函数计算每组中的元素数量。 In addition, if you want each image to be able to be tagged with multiple tags, you'll want to move the image information to its own table, and create a table solely linking the two together. 此外,如果您希望每个图像都能够使用多个标记进行标记,则您需要将图像信息移动到自己的表中,并创建一个仅将两者连接在一起的表。
You can optimize your SQL query to do the work for you: 您可以优化SQL查询以便为您完成工作:
SELECT tags.id, COUNT(id) AS count
FROM tags
INNER JOIN tagged ON tags.id = tagged.tag_id
WHERE tagged.image_id = 34;
GROUP BY tags.id
This will select every tag which is set to be tagged on image id 34
and then fetch the count of total number of images using that tag. 这将选择设置为在图像ID
34
上标记的每个标签,然后使用该标签获取图像总数的计数。 Visual explanation of JOINs JOIN的视觉解释
You are probably at the point where you should have an intermediate table which holds the relationships between the two tables, usually termed " normalization ". 您可能正处于应该拥有一个中间表的位置,该表保存两个表之间的关系,通常称为“ 规范化 ”。
tag_image_table
=========
tag | image
===========
1 | 23
99| 23
which represents image #23 associated to 2 tags. 表示与2个标签相关联的图像#23。
This makes it much easier to select all images associated to a tag, and all tags associated to an image. 这使得选择与标签相关联的所有图像以及与图像相关联的所有标签变得更加容易。
To get the global usage of the tags, you probably need to rejoin to the tagged table, since you are applying the where condition on the original tagged table. 要获取标记的全局用法,您可能需要重新加入标记表,因为您在原始标记表上应用了where条件。 Try this:
尝试这个:
SELECT
t2.id,
COUNT(t3.tag_id) AS global_count,
GROUP_CONCAT(t3.image_id) AS images_with_tag
FROM
tagged t1
JOIN tags t2 ON t2.id = t1.tag_id
LEFT JOIN tagged t3 ON t3.tag_id = t2.id
WHERE t1.image_id = 1
GROUP BY t2.id
ORDER BY global_count DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.