本文介绍了如何实现标签计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的标签在我的数据库desinged是这样的:
I have my tags desinged like this in my database:
Table: Item
Columns: ItemID, Title, Content
Table: Tag
Columns: TagID, Title
Table: ItemTag
Columns: ItemID, TagID
//example -- this is the right sidebar of stackoverflow
c# × 59279
sql × 14885
asp.net-mvc × 9123
linq × 4337
tags × 339
如果我想知道每个标签的数量,如计算器如何计算自己的标签我会怎么做呢?我将执行什么样的查询。我打开这两个常规的SQL和LINQ
if I wanted to know the count of each tag such as how stackoverflow counts their tags how would I do it? What kind of query would I perform. I am open to both regular sql and linq
推荐答案
添加在作为抗衡工作表标签另一列。当您添加或删除的项更新计数器标签(换句话说,如果加上Itemtag连续递增变量表计数器,当删除递减计数器)
Add another column in the table Tag that work as counter. When you add or remove a tag from an item you update the counter (in other words, when add a row on Itemtag increment the counter on Tag table, when remove decrement the counter)
标签添加到项目:
INSERT INTO Itemtag (itemid,tagid) VALUES ('$itemid','$tagid');
UPDATE Tag SET counter=counter+1 WHERE tagid='$tagid';
从项目中删除标签
remove tag from item
DELETE FROM Itemtag WHERE itemid='$itemid' AND tagid='$tagid';
UPDATE Tag SET counter=counter-1 WHERE tagid='$tagid';
检索与反项目代码
retrieve item tags with counter
SELECT t.title, t.counter FROM Itemtag AS it JOIN Tag AS t ON t.idtag=it.tagid
WHERE it.itemid='$itemid'
这篇关于如何实现标签计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!