Reputation: 1295
I have enum field Manufacturer
`manufacturer` ENUM('Manufacturer1','Manufacturer2','Manufacturer3','Manufacturer4') NOT NULL
and I have a array containing multiple manufacturer values. Ho to write query that would select every row that has, lets say for example, manufacturer1 or manufacturer3?
I could write it like this:
SELECT FROM table_name WHERE manufacturer=manufacturer1 OR manufacturer=manufacturer3;
but I am looking for shorter version like:
SELECT FROM table_name WHERE manufacturer=$myArrayOfManufaturers;
I get possible values of manufacturers form checklist so the number of manufacturers can change depending from user choice.
My site is built using CodeIgniter so I tried
$query = $this->db->get_where('products', array('manufacturer' => $data['manufacturers']));
and using forech loop
foreach ($data['manufaturers'] as $man) {
$this->db->or_where('manufacturer'=$man)
}
but can not understand how to set first one as where.
Upvotes: 2
Views: 2999
Reputation: 5316
You can use IN query like
SELECT FROM table_name WHERE manufacturer IN ('Manufacturer1','Manufacturer2')
Upvotes: 5