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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:40:40+00:00 2026-05-14T15:40:40+00:00

For example this query: SELECT `variants`.* FROM `variants` INNER JOIN `variant_attributes` ON variant_attributes.variant_id =

  • 0

For example this query:

SELECT `variants`.* 
  FROM `variants` INNER JOIN `variant_attributes` 
    ON variant_attributes.variant_id = variants.id 
 WHERE (variant_attributes.id IN ('2','5'))

And variant has_many variant_attributes

What I actually want to do is to find which variant has BOTH variant attributes with ID = 2 and 5. Is this possible with MySQL? Bonus Question, is there a quick way to do this with Ruby on Rails, perhaps with SearchLogic?

solution

Thank you Quassnoi for the query you provided, that worked perfectly.

To use on Rails, I used the named_scope below, I think this is simpler to understand for beginners.

Basically named_scope would return {:from => x, :conditions => y} and the lines above were used to setup the y variable.

  named_scope :with_variant_attribute_values, lambda { |values|
    conditions = ["(
            SELECT  COUNT(*)
            FROM    `variant_attributes`
            WHERE   variant_attributes.variant_id = variants.id
                    AND variant_attributes.value IN (#{values.collect { |value| "?" }.join ", "})
            ) = ?
    "]
    conditions = conditions + values + [values.length]
    {
    :from => 'variants', 
    :conditions => conditions
  }}
  • 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-14T15:40:41+00:00Added an answer on May 14, 2026 at 3:40 pm

    Assiuming that variant_attributes (variant_id, id) is unique:

    SELECT  `variants`.*
    FROM    `variants`
    WHERE   (
            SELECT  COUNT(*)
            FROM    `variant_attributes`
            WHERE   variant_attributes.variant_id = variants.id
                    AND variant_attributes.id IN ('2','5')
            ) = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do this query: SELECT * FROM mail WHERE tag_draft = 0
Consider this query: SELECT F1,F2 FROM TABLE GROUP BY F1 Selecting F1 is valid,
I have this query: SELECT * FROM table WHERE key LIKE '1,2,3,%' OR key
Is there a standard way to convert between TVarRec and Variant values? I want
I have this query to count views in a table, by date. It obviously
My query: SELECT *, contacts.createdAt AS contactcreatedAt, contacts.updatedAt AS contactupdatedAt, bidresponses.itemid AS bidresponseitemid, bidresponses.personid
I have a SQL query I'm having trouble creating using NHibernate Criteria: SELECT ID,
I'd like to make a query where I can retrieve an interval of 10
I am creating RPG type game. I pull out player's information in every page
1. So if I search for the word ball inside the toys table where

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.