【问题标题】:How to write custom orderby in drupal 7 db_select query?如何在 drupal 7 db_select 查询中编写自定义 orderby?
【发布时间】:2012-11-19 19:16:15
【问题描述】:

以下查询的等效 db_select 查询是什么,

$nids = array(5, 3, 1, 4, 2);

$sql = "SELECT
          nid, title, FIELD(nid, :nids) sortkey
          FROM
            {node}
          WHERE
            nid IN (:nids)
            AND type = :type
            AND status = 1
          ORDER BY sortkey";

$result_nid = db_query($sql, array(':nids' => $nids, ':type' => 'article'))
                ->fetchAll();

【问题讨论】:

    标签: mysql drupal drupal-7


    【解决方案1】:

    这是我从 drupal.org 获取的高级查询。

    你应该很容易适应

    $query = db_select('node', 'n');
    
    $query->join('users', 'u', 'n.uid = u.uid'); //JOIN node with users
    
    $query->groupBy('u.uid');//GROUP BY user ID
    
    $query->fields('n',array('title','created'))//SELECT the fields from node
    ->fields('u',array('name'))//SELECT the fields from user
    ->orderBy('created', 'DESC')//ORDER BY created
    ->range(0,2);//LIMIT to 2 records
    
    $result = $query->execute();
    

    希望对你有帮助

    公关

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-22
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多