복붙노트

[SQL] IN 절에서 모든 값을 일치

SQL

IN 절에서 모든 값을 일치

일치 IN 절에서 모든 값을 확인하는 방법이 있나요?

예:

IN (5,6,7,8) : 나는로 사용할 수 있습니다.

나는 그것이 좋아하고 여러 행에서 작동해야합니다.

최신 정보: 나는 맞는 매개 변수를 지정한 DB에서 목록 기업이 필요합니다. 회사 및 분류는 많은 관계에 많은입니다. 나는 YII 프레임 워크를 사용하고 있습니다. 그리고 이것은 내 컨트롤러의 코드는 다음과 같습니다

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,
    ));
}

해결법

  1. ==============================

    1.당신이 뭔가를 할 수 있습니다 :

    당신이 뭔가를 할 수 있습니다 :

    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
    

    당신이 당신의 스키마와 일부 샘플 데이터를 제공하는 경우, 나는 더 적절한 해답을 제공 할 수 있습니다.

    SQL 바이올린 예

  2. ==============================

    2.

     SELECT ItemID
         FROM ItemCategory
            WHERE (
                   (CategoryID = 5) OR 
                   (CategoryID = 6) OR 
                   (CategoryID = 7) OR 
                   (CategoryID = 8)
                  )
         GROUP BY ItemID
     HAVING COUNT(DISTINCT CategoryID) = 4
    
  3. from https://stackoverflow.com/questions/11636061/matching-all-values-in-in-clause by cc-by-sa and MIT license