在MySQL中循环查询

时间:2018-03-17 09:58:01

标签: php mysql

Drupal 7 DB select

我在这里得到一个数组:

Array ( [71] => adad [85] => erer )

如何在以下查询中循环上述数组?

function get values($array){
 $q =  db_select('node', 'n');
  $q->join('ab', 'f', 'n.nid = f.entity_id');
  $rooms_data = $q->condition('n.type', 'book');
if($room_selected!=null)
{
  $rooms_data = $q->condition('n.nid', $array); **//assumption//**
}
   $rooms_data = $q->fields('n', array('nid', 'title'))
    ->fields('f', array('feesid'))
    ->orderBy('n.title', 'ASC')
    ->execute();
}

假设有类似的东西:

->wherein($array as $a)
{
$q->condition('n.nid',$a['value']);
}

1 个答案:

答案 0 :(得分:2)

您可以将"IN"用作SelectQuery::condition$operator,并使用标量值数组$value。为此,您可以使用array_column()来提取这些值:

function get_values($array, $room_selected) {
    $q =  db_select('node', 'n');
    $q->join('ab', 'f', 'n.nid = f.entity_id');
    $q->condition('n.type', 'book');
    if ($room_selected != null)
    {
        $values = array_column($array, 'value') ;
        $q->condition('n.nid', $values, 'IN') ;
    }
    $rooms_data = $q->fields('n', array('nid', 'title'))
        ->fields('f', array('feesid'))
        ->orderBy('n.title', 'ASC')
        ->execute();
}