Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching all values in IN clause

Tags:

sql

mysql

yii

Is there a way to ensure all values in an IN clause are matched?

Example:

I can use IN as: IN (5,6,7,8).

I need it to work like an AND across multiple rows.

UPDATE: I need this to list companies from db that fit specified parameters. Companies and taxonomy are MANY TO MANY relation. I'm using Yii framework. And this is the code of my controller:

public function actionFilters($list) {     $companies = new CActiveDataProvider('Company', array(         'criteria' => array(             'condition'=> 'type=0',             'together' => true,             'order'=> 'rating DESC',             'with'=>array(             'taxonomy'=>array(                 'condition'=>'term_id IN ('.$list.')',                 )             ),         ),     ));     $this->render('index', array(         'companies'=>$companies,     )); } 
like image 474
Dima Knivets Avatar asked Jul 24 '12 17:07

Dima Knivets


People also ask

How do I match multiple values in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);

Can I put count in WHERE clause?

SQL SELECT COUNT() can be clubbed with SQL WHERE clause.

WHERE clause pass multiple values?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.


1 Answers

You can do something like this:

select ItemID from ItemCategory where CategoryID in (5,6,7,8) <-- de-dupe these before building IN clause group by ItemID having count(distinct CategoryID) = 4 <--this is the count of unique items in IN clause above 

If you provide your schema and some sample data, I can provide a more relevant answer.

SQL Fiddle Example

like image 128
D'Arcy Rittich Avatar answered Oct 08 '22 11:10

D'Arcy Rittich