mysql查询到drupal查询

时间:2012-07-26 11:34:34

标签: mysql drupal-7

请任何人都可以将此查询转换为drupal db_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"; 

谢谢

1 个答案:

答案 0 :(得分: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);
  }

然后你只需要执行并获取结果。