以下查询的等效 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();
最佳答案
这是我从 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();
希望对你有帮助
公关
关于mysql - 如何在drupal 7 db_select查询中编写自定义orderby?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13460548/