Today i am demonstrating how to write dynamic query in drupal 7,
$query = db_select('node','n')
->fields('n',array('title','nid'))
->condition('n.status',1)
->execute();
It simply returns the object of the current query,
foreach($query as $result){
echo $result->title;
}
How To Use database function in dynamic query
$tbl = 'secc_rural';
$house_hold_id = '09280030005';
$query = db_select($tbl.".individual_21", "n");
$query->fields('n',array('name','ahl_tin','ahl_tin','fathername','name_sl','mothername','occupation','yob','hof','genderid','relation','house_hold_id'));
//$query->addExpression('substr(house_hold_id,1,11)',$house_hold_id);
$query->where('substr(house_hold_id,1,11) = :hid',array(':hid' => $house_hold_id));
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(20);
$result = $query->execute();
=========================================================
GROUP BY CLAUSE WITH HAVING CONDITION:-
$res = db_select('ssid_generated_data.uwssid','gb');
$res->groupBy('gb.ssid_batch');
$res->addExpression('count(ssid_batch)','total_records');
$res->havingCondition('gb.statecode','32');
$res->havingCondition('gb.districtcode','09');
$res->groupBy('gb.statecode');
$res->groupBy('gb.districtcode');
$res->groupBy('gb.tehsilcode');
$res->groupBy('gb.towncode');
$res->fields('gb',array('ssid_batch','statecode','districtcode','tehsilcode','towncode'));
$all = $res->execute()->fetchAll();
You have to add as many fields on groupby as you choose as a field options
for further references: Visit
http://browse-tutorials.com/tutorial/sql-query-examples-drupal-7
$query = db_select('node','n')
->fields('n',array('title','nid'))
->condition('n.status',1)
->execute();
It simply returns the object of the current query,
foreach($query as $result){
echo $result->title;
}
How To Use database function in dynamic query
$tbl = 'secc_rural';
$house_hold_id = '09280030005';
$query = db_select($tbl.".individual_21", "n");
$query->fields('n',array('name','ahl_tin','ahl_tin','fathername','name_sl','mothername','occupation','yob','hof','genderid','relation','house_hold_id'));
//$query->addExpression('substr(house_hold_id,1,11)',$house_hold_id);
$query->where('substr(house_hold_id,1,11) = :hid',array(':hid' => $house_hold_id));
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(20);
$result = $query->execute();
=========================================================
GROUP BY CLAUSE WITH HAVING CONDITION:-
$res = db_select('ssid_generated_data.uwssid','gb');
$res->groupBy('gb.ssid_batch');
$res->addExpression('count(ssid_batch)','total_records');
$res->havingCondition('gb.statecode','32');
$res->havingCondition('gb.districtcode','09');
$res->groupBy('gb.statecode');
$res->groupBy('gb.districtcode');
$res->groupBy('gb.tehsilcode');
$res->groupBy('gb.towncode');
$res->fields('gb',array('ssid_batch','statecode','districtcode','tehsilcode','towncode'));
$all = $res->execute()->fetchAll();
You have to add as many fields on groupby as you choose as a field options
for further references: Visit
http://browse-tutorials.com/tutorial/sql-query-examples-drupal-7
Comments
Post a Comment