Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:00:24+00:00 2026-05-26T15:00:24+00:00

I have a table tags with fields tag_id and name . I have a

  • 0

I have a table tags with fields tag_id and name. I have a junction table tag_map with fields tag_id and vid_id that connects table tags to table video (via vid_id).

When I insert a new tag for a vid_id, I delete all my entries in tag_map for that vid_id, if there was only one entry in tag_map I also want to delete that tag in table tags to prevent ghost entries (tags entries without a corresponding tag_map entry).

The issue is, I cannot seem to accurately obtain this count. For example if I have 3 entries in tag_map, each having 1 corresponding tag in tags, I get count for each tag as 1,0,0. Am I using the wrong query for this purpose?

//find all tags for given vid_id
$sql = 'SELECT tag_map.*, tags2.name AS tag_name
FROM tag_map
INNER JOIN tags2 ON tags2.tag_id = tag_map.tag_id
INNER JOIN video ON video.vid_id = tag_map.vid_id
WHERE tag_map.vid_id=?';
$stmt_tags = $conn->prepare($sql);
$result=$stmt_tags->execute(array($vid_id));

while ($row = $stmt_tags->fetch(PDO::FETCH_ASSOC)) {
$tag_name=$row['tag_name'];

//for each tag check how many rows exist in tag_map COUNT IS NOT ACCURATE
    $sql = 'SELECT *, COUNT(tags2.name) as cnt
FROM tag_map
JOIN tags2 ON tags2.tag_id = tag_map.tag_id
JOIN video ON video.vid_id = tag_map.vid_id
WHERE tags2.name=?';
$stmt_tags2 = $conn->prepare($sql);
$result=$stmt_tags2->execute(array($tag_name));

while ($row2 = $stmt_tags2->fetch(PDO::FETCH_ASSOC)) {
    echo $cnt=$row2['cnt'];
}

if ($cnt==1){
  $sql = "DELETE FROM tag_map WHERE vid_id = ?";
$stmt14 = $conn->prepare($sql);
$result=$stmt14->execute(array($vid_id));
  $sql = "DELETE FROM tags2 WHERE tags2.name = ?";
$stmt14 = $conn->prepare($sql);
$result=$stmt14->execute(array($tag_name));
}
else {
      $sql = "DELETE FROM tag_map WHERE vid_id = ?";
$stmt14 = $conn->prepare($sql);
$result=$stmt14->execute(array($vid_id));
}
}

EDIT for inserting into many to many

foreach($variable as $tag) {

    $sql = 'SELECT *, COUNT(tags2.name) as cnt
FROM tag_map
JOIN tags2 ON tags2.tag_id = tag_map.tag_id
JOIN video ON video.vid_id = tag_map.vid_id
WHERE tags2.name=?';
$stmt_tags2 = $conn->prepare($sql);
$result=$stmt_tags2->execute(array($tag));
while ($row = $stmt_tags2->fetch(PDO::FETCH_ASSOC)) {
    $cnt=$row['cnt'];
    $exist_tag_id=$row['tag_id'];
}

$id=md5(uniqid());
$tag_id=md5(uniqid());

if ($cnt==0){
    $sql="INSERT into tag_map (id,vid_id,tag_id) VALUES (?,?,?)";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array($id,$vid_id,$tag_id));
    $sql="INSERT into tags2 (tag_id,name) VALUES (?,?)";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array($tag_id,trim($tag)));
}
else {
$sql="INSERT into tag_map (id,vid_id,tag_id) VALUES (?,?,?)";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array($id,$vid_id,$exist_tag_id));
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:00:25+00:00Added an answer on May 26, 2026 at 3:00 pm

    You really don’t even need to worry about the count. Delete from tag_map and then:

    DELETE t 
        FROM tags2 t
            LEFT JOIN tag_map tm
                ON t.tag_id = tm.tag_id
        WHERE tm.tag_id IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Tags table whose schema consists of only ID and Name (unique).
I have css that works on all tr tags for the table. so in
I have a table of name value pairs where I am storing tags: TAGID
I have an app that has posts, tags, and a tag_ref link table. I
Suppose I have table tags which has a field count that indicates how many
If I have a table on my DB called product_tags with 2 fields: tag_id
Let's pretend that there's a tags table with a unique field called name .
I have a table : tags(tagname,entryid,stamp) and i want to make a section for
i have a database table of website links. i have another table of tags,
Suppose I have a tags table with two columns: tagid and contentid . Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.