How to select records from table1 which have table3 options in table2?
Let's say, I have three tables:
- table1: contains various records;
- table2: contains what records have what options;
- table3: contains options;
table2 contains foreign key of table1, and table3 - thus specifying which table1 record has witch table3 option. table1 records can have multiple table3 options;
I'm trying to select table1 records by specifying their options, I need to be able to select any option\any option combination and display all table1 records which have this option combination, it can have additional options, but i开发者_Go百科t must have the specified ones...
How would you write/build such a query?
note: I'm specifying what options to use from form..
imagine - table 1 contains products and table3 contains various details about products
User submits a form selecting check-boxes thus showing what details a product should have and I need to get all records which have these options ...
foreach($table1record as $detail){
if($this->input->post('detail_{$detail['id']})){
//option selected, continue building query ...
}
}
How about this?
SELECT t1.*
FROM table1 as t1
INNER JOIN table2 as t2
WHERE t2.SomeColumn IN (SELECT SomeOtherCol FROM table3);
精彩评论