I'm trying to extract data from a many to many table configuration.
I'm sorry if this explanation is vague, but it's the best I can do without a lengthy explanation of the whole inner workings of my program.
I have 2 modules in an application.
Module one shows information relevant to a group of items.
And it strips out information that is not present in all items. (lowest common denominator)
So...
Item one is red and green.
Item two is green and blue.
And item three is just green.
So table one would be the list of items and table two would be the list of colors.
And of course, a third table holds the relationships.
I'm trying to get the module to only show elements from table two that are present in all items from table one.
In this case, it would strip the shown elements to just green because that's the only common element.
I tried doing this programatically and it was a nightmare to look at and debug. That and this will be a mobile app so all the for loops will really lag my program. I know that there has to be a way to do it with SQL, but after several hours researching, I still can't seem to wrap my head around how.
Thanks.
UPDATE for clarity:
OK, I made 2 mistakes in my question. First off, I'm not looking for common quantity, I'm looking for common VALUE. So I only want colors that exist in all three items and to discard the rest. Second I should have been more specific. I'm developing an app for the Android phones, so I'm using Java and SQLite.
Here's what I have so far in the original form, not the color example I used. In this code tag would equal color and other would equal item...
SELECT DISTINCT tag.name
FROM other_tag_relationship AS otr
JOIN tag ON otr.tag_id = tag._id
WHERE otr.other_id in (2, 3, 4);
This obviously returns a distinct list of all tags for all of the given others.
What I want to do now is show only the tags that are available in ALL others and discard the rest. Like remove any tags from 2 that aren't also in 3 and 4 for example. Also, the list (2, 3, 4) would be generated dynamically at run time.
Thanks again.