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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:31:28+00:00 2026-05-23T02:31:28+00:00

I have models(tables) in my database with table and fields name like tags (id,

  • 0

I have models(tables) in my database with table and fields name like

tags                 (id, name)
taggings             (id, tag_id, taggable_id, taggable_type, context)
employment_histories (id, user_id, grades, subjects, my_interests )
users                (id)

taggable_id is actually employment_histories_id and context can either be grade or subjects or my_interests

now I have array of tags e.g. g={“9th”,”10th”}
and I want to get users, only whose tags are all matching to the above g array.

I’ve written the query below:

SELECT DISTINCT users.* FROM `users` 
LEFT OUTER JOIN `employment_histories` 
  ON `employment_histories`.`user_id` = `users`.`id` 
LEFT OUTER JOIN `taggings` 
  ON `employment_histories`.`id` = `taggings`.`taggable_id` 
  AND `taggings`.`taggable_type` = 'EmploymentHistory' 
LEFT OUTER JOIN `tags` ON taggings.context = 'subjects' 
WHERE tags.name='9th' OR tags.name='10th'

but it gives me those users too, which match any of the tags, however I want that it will return only that user who match all the two tags

Suppose that tags 9th and 10th have tag id 9 and 10 then what i want that it will only return the taggable_id(which is employmenthistories.id) who has common taggable_id for these two tag_id (that is 9 and 10) in taggings table

for example i have two user tariq and kamal and both of these users have 9th tag common but kamal dont have tag 10th so want query which if passed these two tags should return only tariq whose tags are all macthing these two tags but users like kamal which match any of the tags should be filtered too

  • 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-23T02:31:29+00:00Added an answer on May 23, 2026 at 2:31 am

    From php chat room:

    SELECT
    users.* ,
    count(*) AS count
    FROM users
        LEFT JOIN employment_histories ON  users.id = employment_histories.user_id
        LEFT JOIN tagging ON tagging.taggable_id = employment_histories.id
        LEFT JOIN tags ON tags.id = tagging.tag_id
    WHERE tags.name = "9th"
           OR tags.name = "10th"
    GROUP BY users.id
    HAVING count = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have the database like ======= Group ======== id name ======= Member ======== id
I have a database structure that has a Person table which contains fields such
I have models corresponding to database tables. For example, the House class has color,
I've got Django tables like the following (I've removed non-essential fields): class Person(models.Model): nameidx
Suppose I have an Orders table in my database and a corresponding model class
Here is a simplified version of my database model. I have two tables: Image,
I have a n...n structure for two tables, makes and models . So far
If i have two tables Books, CDs with corresponding models. I want to display
I have two models, say Product and Bundle. products table is related to product_prices

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.