Drupal 7 开发内部培训资料, 分头诗人

drupal sql查询语句

赞成!
0
否决!

我在后台内容类型里新加了一些字段,如邮箱,电话,住址,公司等,然后去添加内容里给这些字段加了些数据,现在我想自己写SQL语句查询其中的三个字段,比如邮箱,电话,和住址,这个SQL语句怎么写啊。。。我看数据库里是一个字段一个表的呢?!晕了。。。。

1 个回答

赞成!
0
否决!
<?php
function article() {	
	$output = '';
	$query = db_select('node','n')->extend('PagerDefault');//PagerDefault是分页
	$query->leftJoin('field_data_field_email','e','n.nid = e.entity_id');//字段名: field_email 
	$query->leftJoin('field_data_field_address','a','n.nid = a.entity_id');//字段名: field_address
	$query->leftJoin('field_data_field_company','c','n.nid = c.entity_id');//字段名: field_company
	$query->fields('n')->fields('e')->fields('a')->fields('c');
	$query->condition('n.status', 1)//condition是条件,如sql语句的where
		->condition('n.type', 'article')//article 是内容类型
		->limit(10);
	$query->orderBy('created', 'DESC');
	$objects = $query->execute()->fetchAll();
	$rows = array();
	foreach ($objects as $key=>$object) {
		$row = array();
		$row['nid'] = $object -> nid;
		$row['email'] = $object -> field_email_value;//field_email_value是email表中的值
		$row['address'] = $object -> field_address_value;
		$row['company'] = $object -> field_company_value;
		$row['created'] = format_date($object -> created);
		
		$rows[] = $row;
	}
	$output .= theme('table', array('rows' => $rows));
	$output .= theme('pager');
	return $output;
}