【问题标题】:Drupal db_select - How to combine UNION with LIMIT properly?Drupal db_select - 如何正确结合 UNION 和 LIMIT?
【发布时间】:2013-06-05 15:34:24
【问题描述】:

我正在尝试从数据库中获取按类别划分的内容。我严格要求最多 4 个“人”类型的条目和另外三个“组织”类型的条目。

我试着这样做:

    $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();

但这两种方法只返回4个人,没有组织,我的意思是永远不会..

如果你能提供帮助,谢谢!

【问题讨论】:

    标签: database drupal-7 limit union


    【解决方案1】:

    查询中的联合语句实际上是从第一个查询中获取记录,并将第一个查询的结果记录合并到第二个查询结果记录。

    例如第一种情况不要在联合查询中使用限制,它会给出所有记录。

     $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();
    

    【讨论】:

      猜你喜欢
      • 2010-11-27
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      • 2021-11-09
      • 2013-04-07
      相关资源
      最近更新 更多