更新: 我需要获得标签字段的所有值!
我的查询:
$query = db_select('node', 'node');
$query->fields('tagsdata',array('name'));
$query->fields('node', array('nid'));
$query->leftJoin('field_data_field_tags', 'tags', 'tags.entity_id = node.nid');
$query->leftJoin('taxonomy_index', 'tagsindex', 'tagsindex.nid = tags.entity_id');
$query->leftJoin('taxonomy_term_data','tagsdata','tagsdata.tid = tags.field_tags_tid AND node.nid = tagsindex.nid');
$result = $query->execute();
while( $record = $result->fetchAssoc() ) {
$items[] = $record;
}
和我的代码:
//SORT
array_multisort(array_column($items, 'nid'), $items);
foreach ($items as $row) {
$hash[$row[nid]] = $row;
}
$resultfinal = ($hash);
// END SORT
foreach($resultfinal as $finalarrays)
{
$tags=$finalarrays['name'];
print_R ($tags);
}
使用上面的代码只返回一个和第一个标签值,我需要打印所有这些!
答案 0 :(得分:0)
您可以使用GROUP_CONCAT mysql函数获取逗号所包含的所有值:
$result = db_query("SELECT tags.entity_id as nid, GROUP_CONCAT(t.name) as tdata FROM field_data_field_tags
INNER JOIN taxonomy_term_data t ON t.tid = tags.field_tags_tid
WHERE tags.entity_type = :type GROUP BY tags.entity_id",
array(':type' => 'node'))->fetchAllKeyed();
注意:有时你有太多的字符串连接,所以你需要增加限制之前:
db_query('SET SESSION group_concat_max_len=10000');
$result = db_query("SELECT tags.entity_id as nid, GROUP_CONCAT(t.name) as tdata FROM field_data_field_tags
INNER JOIN taxonomy_term_data t ON t.tid = tags.field_tags_tid
WHERE tags.entity_type = :type GROUP BY tags.entity_id",
array(':type' => 'node'))->fetchAllKeyed();
https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
foreach($result as $nid => $tags) {
echo $nid . ' : '.$tags;
}