2

私は以下のようなテーブルを持っています:

node_name              id            term_name
----------------------------------------------
test1                  001           physics 
test1                  001           maths    
test1                  001           chemistry    
test2                  002           physics    
test2                  002           maths

id用語名の組み合わせが与えられた場合、セットに指定された用語名のみが含まれているすべての行を検索したいと考えています。

たとえば、用語名が物理学と数学の場合、私の出力は次のようになります

node_name              id            term_name
----------------------------------------------
test2                  002           physics   
test2                  002           maths

Id セット001にも含まれchemistryているため、含めるべきではありません。

4

3 に答える 3

1

あなたの質問:同じIDを持つ他の行が存在しないすべての行を取得しterm_namesます

SELECT * FROM <table> x WHERE
  term_name IN ('physics','maths') AND
  NOT EXISTS (SELECT * FROM <table> WHERE id=x.id AND term_name NOT IN ('physics','maths'))
于 2011-12-22T07:07:57.430 に答える
0

まず、クエリを解析して、PHP の「&」を SQL の「OR」演算子に変換する必要があります。

//Parse the query
        $arr = explode('&',$query);
    $where = '';
//get the term count
    $count = count($arr);
    foreach($arr as $value){
    $where .= "term_name = '" . $value . "' OR";
    }
    //Remove last or
    $where = rtrim($where,'OR');

次に : L を使用

"select node_name ,count(1) as Total from my table where $where
group by node_name
having Total =" . $count

ついに :

クエリは次の形式にする必要があります。

select x,count(1) as total from mytable where field1 = 'term1' or field1 = 'term2' having total = 2
于 2011-12-22T07:07:37.270 に答える
0

これを行う 1 つの可能な方法:

select id, node_name 
from nodes join 
  (select id, 
       count(*) 
from nodes
  where node_name in ('physics','math')
group by id
having count(*) = 2 // this is set when generating the query ) as eligible_nodes
  on nodes.id = eligible_nodes.id
于 2011-12-22T14:31:26.060 に答える