简体   繁体   English

从表中检索所有数据,然后计算另一个中的结果数

[英]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, 所以简而言之,我需要做以下事情,

  1. Grab all tags for the current image displayed, eg id = 34 . 抓取显示的当前图像的所有标签,例如id = 34
  2. Count how many times the tag has been used. 计算标签使用次数。
  3. Order the tag list by these results, going from high to low. 按照这些结果对标签列表进行排序,从高到低。

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: 这是它的确切作用:

  • The join takes each entry in tagged, and pairs it with its corresponding tag. 连接将标记每个条目,并将其与相应的标记配对。
  • We then group by the tag id, so that we only get one entry for each id. 然后我们按标签ID进行分组,这样我们每个id只能获得一个条目。
  • The COUNT function counts how many elements are in each group. COUNT函数计算每组中的元素数量。
  • We finally sort it in descending order by the count, giving the most popular tags on top. 我们最终按计数降序排序,给出最热门的标签。

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.

相关问题 laravel 4无法检索所有结果? 数据透视表 - laravel 4 Cannot Retrieve ALL results ? pivot table Many to Many 如何从一个表中检索数据并计算另一个表中的行数,然后在while循环或HTML表中一起显示它们? - How to retrieve data from a table and count number rows from another table then show them together in a while loop or HTML table? 计算查询的行在所有表上出现多少次 - count how many times a row from a query appear on all the table 如何从一个表中检索数据,并如何使用外键从另一个表中检索数据? - How can I retrieve data from a table and using a foreign key retrieve data from another table? 从多对多表中获取所有数据 - Getting all data from a many to many table 如何从学说中的联接表中检索结果 - How to retrieve results from joined table in doctrine 如何获取具有多对多关系的表的所有结果 - How to get all results of a table with Many To Many relationship cakephp从另一个表中将数据检索到控制器中 - cakephp retrieve data into controller from another table 如何从 MySQL 中的另一个表中检索数据,同时从第一个表的列中获取数据? - How to retrieve data from another table in MySQL while having a data from column from the first table? 如何从一个表中检索信息以订购另一个表的结果 - How can I retrieve information from one table to order the results of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM