1

誰でもこのクエリをdrupaldb_selectに変換できますか

    $query="SELECT DISTINCT 'supplier_id','supplier_name','supplier_address','supplier_city', 'supplier_pincode', 'supplier_state', 'supplier_country' FROM pmai_supplier_directory WHERE ";
foreach($list as $k=>$value)
{
    $query.=" supplier_interest LIKE '%".$value."%' OR";
}
$query=substr($query,0,strlen($query)-2);
echo $query.=" ORDER BY supplier_name"; 

ありがとうございました

4

1 に答える 1

4

したがって、クエリは次のとおりです。

SELECT DISTINCT 'supplier_id','supplier_name','supplier_address','supplier_city',
   'supplier_pincode', 'supplier_state', 'supplier_country' 
FROM pmai_supplier_directory 
WHERE supplier_interest LIKE '%foo%' 
   OR supplier_interest LIKE '%bar%'
   OR supplier_interest LIKE '%baz%'
ORDER BY supplier_name"; 

これはそのように行うことができます:

$list = array('foo','bar','baz');
$query = db_select('pmai_supplier_directory','pmsp')

  ->fields('pmsp',array(
    'supplier_id',
    'supplier_name',
    'supplier_address',
    'supplier_city',
    'supplier_pincode',
    'supplier_state',
    'supplier_country'
  ))
  ->orderBy('supplier_name')
  ->distinct();

  $orCond = db_or();$elt = 0;
  foreach($list as $k => $value) {
    $orCond->condition('pmsp.supplier_interest','%'.$value.'%','LIKE');
    $elt++;
  }
  if ($elt) {
    $query->condition($orCond);
  }

次に、結果を実行してフェッチするだけです。

于 2012-07-26T12:05:38.277 に答える