Database Drupal db_select-如何正确地将UNION与LIMIT结合起来?
我正在尝试从按类别划分的数据库中获取内容。我要严格地最多4个“人”类型的条目和3个“组织”类型的其他条目 我试着这样做:Database Drupal db_select-如何正确地将UNION与LIMIT结合起来?,database,drupal-7,limit,union,Database,Drupal 7,Limit,Union,我正在尝试从按类别划分的数据库中获取内容。我要严格地最多4个“人”类型的条目和3个“组织”类型的其他条目 我试着这样做: $query = db_select('node', 'n') ->fields('n', array('title','type')) ->fields('i', array('field_image_fid')) ->fields('f', array('uri')) ->condition('n.title
$query = db_select('node', 'n')
->fields('n', array('title','type'))
->fields('i', array('field_image_fid'))
->fields('f', array('uri'))
->condition('n.title', '%'. db_like($keys) . '%', 'LIKE')
->condition('type', array('people'))
->range(0,4);
$query->leftJoin('field_data_field_image', 'i', 'i.entity_id = n.nid');
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_image_fid');
$query2 = db_select('node', 'n')
->fields('n', array('title','type'))
->fields('i', array('field_image_fid'))
->fields('f', array('uri'))
->condition('n.title', '%'. db_like($keys) . '%', 'LIKE')
->condition('type', array('organization'))
->range(0,4);
$query2->leftJoin('field_data_field_image', 'i', 'i.entity_id = n.nid');
$query2->leftJoin('file_managed', 'f', 'f.fid = i.field_image_fid');
$query->union($query2, 'UNION');
$result = $query
->execute();
问题是,此查询只返回前三次出现的人员或组织的组合。因此,如果查询返回了三个人,我将无法看到任何组织
我也试过这样的方法:
$query = db_query('
SELECT p.title,p.type
FROM node as p
WHERE p.type = :type
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query2 = db_query('
SELECT o.title,o.type
FROM node as o
WHERE o.type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query->union($query2, 'UNION');
$result = db_query('
SELECT title,type
FROM {node}
WHERE type = :type
LIMIT 4
UNION ALL
SELECT title,type
FROM {node}
WHERE type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
或者像这样:
$query = db_query('
SELECT p.title,p.type
FROM node as p
WHERE p.type = :type
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query2 = db_query('
SELECT o.title,o.type
FROM node as o
WHERE o.type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query->union($query2, 'UNION');
$result = db_query('
SELECT title,type
FROM {node}
WHERE type = :type
LIMIT 4
UNION ALL
SELECT title,type
FROM {node}
WHERE type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
但这两种方法只返回4个人,没有组织,我的意思是永远不会
如果你能帮忙,谢谢你 查询中的Union语句实际从第一个查询中获取记录,并将第一个查询的结果记录合并到第二个查询结果记录 例如,第一种情况在联合查询中不使用limit,而是给出所有记录
$query1 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('people'));
$query2 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('organization'));
$query = Database::getConnection()
->select($query1->union($query2))
->fields(NULL, array('nid','title', 'type'))
->orderBy('nid');
$result = $query->execute()->fetchAll();
从上面的查询中,它会在第二个查询的记录之前追加第一个查询的记录
根据您的示例,如果您希望从人员内容类型获取4条记录,从组织内容类型获取3条记录使用以下查询来解决您的问题
$query1 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('people'))
->range(0,4);
$query2 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('organization'));
->range(0,7);
$query = Database::getConnection()
->select($query1->union($query2))
->fields(NULL, array('nid','title', 'type'))
->orderBy('nid');
$result = $query->execute()->fetchAll();