I am trying to query a few tables however to simplify it ill make it specific to one table.
have a look at the table view i have the attribute_id column and each row has an attribute id and some sort of value. I would like to select values specific to an attribute id from the whole table.
To make it easier to understand in the values on the table above i would like all the rows in the table where all rows in which the “text_value” for “attribute_id” 1 is 02067 and the text value for “attribute_id” 2 is HEERA BROWN BASMATI RICE. I would like both of these conditions to match one product. One product is made unique by “product_id”
Example
from this set of results i would like a return of all the rows where rows with attribute_id 25 have an integer_value of 11 and rows with attribute_id of 24 have a value of 6 (currently all do but in the future they wont) both conditions must match for one product_id for that product_id rows to be returned.
If you need more clarification please ask thank you.
2
Answers
This is a reasonably basic JOIN – I think you would benefit from reading up about SQL and how to construct joins as it will give you more ideas.
In your example, if your table is called MyTable then you would use:
This will give you all rows where you have the combination of attributes you have requested. You would not have to use a UNION query, but doing so allows you to select data from different tables which appears to be what you are asking for.
Given the edit to the original question and further comments I think I know what the question is asking, but it has not yet been confirmed. The OP appears to be asking for records where the same ‘product_id’ is listed as satisfying the two sets of related criteria and only if it matches both should it be returned.
This can be done by joining the table to itself, and then setting criteria against each side of the join.
In this example, again assuming that the name of the table is ‘MyTable’:
There are other ways to do it – for example using IN (SELECT …) queries: