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 6048073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:25:21+00:00 2026-05-23T07:25:21+00:00

I have a media table and a tag_media table. The tag_media table is a

  • 0

I have a media table and a tag_media table. The tag_media table is a relationship table that contains the columns id_tag and id_media. A single media file can be tagged with multiple tags. Something like:

tag_media:
    id_tag
    id_media

media:
    id_media
    (etc, etc)

I need a query that will allow me to fetch all media that was tagged with a set of mandatory tags and a set of optional tags so that I can guarantee that the returned media were tagged with ALL the mandatory tags and AT LEAST one of the optional tags.

How can I do this?

  • 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-23T07:25:22+00:00Added an answer on May 23, 2026 at 7:25 am
    SELECT m.*
    FROM media m
      INNER JOIN (
        SELECT id_media
        FROM tag_media
        GROUP BY id_media
        HAVING COUNT(id_tag IN (required1, required2) OR NULL) = 2
           AND COUNT(id_tag IN (option1, option2, option3) OR NULL) >= 1
      ) t ON m.id_media = t.id_media
    

    This assumes that one media item cannot have duplicate tags.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a simple relationship like this: Table: Media , Columns: Id, Url,
I'm trying to link one table to itself. I have media groups which can
Is there any way we can hide column of display table,we have media=none which
I have a MYSQL database table called 'wp_quotes' with a field that contains a
Sorry the title isn't more help. I have a database of media-file URLs that
if i have two media players in form1 and form2 respectively....how can i control
I have a function that searches a database table via ajax. This is the
I have 2 tables: Media and Galleries. Media contains thousands of records of different
I have a table titled videos . In it there are three columns: media_id
i have a table with record given below. What i want is single select

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.