Luk*_*101 7 c# sql linq asp.net-mvc tagging
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
Run Code Online (Sandbox Code Playgroud)
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 上添加行时,标签表上的计数器会增加,当删除时,计数器会减少)
为项目添加标签:
INSERT INTO Itemtag (itemid,tagid) VALUES ('$itemid','$tagid');
UPDATE Tag SET counter=counter+1 WHERE tagid='$tagid';
Run Code Online (Sandbox Code Playgroud)
从项目中删除标签
DELETE FROM Itemtag WHERE itemid='$itemid' AND tagid='$tagid';
UPDATE Tag SET counter=counter-1 WHERE tagid='$tagid';
Run Code Online (Sandbox Code Playgroud)
使用计数器检索项目标签
SELECT t.title, t.counter FROM Itemtag AS it JOIN Tag AS t ON t.idtag=it.tagid
WHERE it.itemid='$itemid'
Run Code Online (Sandbox Code Playgroud)