Query on custom drupal 7 field I'm facing problem in Drupal query for order by custom field
Here is my query

$result = db_query('SELECT n.nid FROM {node} n WHERE n.type = :ntype', array(':ntype' =&gt; 'portfolio'));


this work fine but when I change that to this

$result = db_query('SELECT n.nid FROM {node} n WHERE n.type = :ntype order by n.field_weight DESC', array(':ntype' =&gt; 'portfolio'));


That query not working. It saying field not found?
Try something like (supposing "weight" module is used and weights are stored in the "weight_weights" table - to be adapted for another module ) :

$query = db_select('node', 'n');
$query-&gt;fields('n', array('nid', 'type'));
$query-&gt;join('weight_weights', 'w', 'n.nid = w.entity_id');//join can not be chained
$query-&gt;condition('n.type', 'portfolio', '=');
$query -&gt;orderBy('w.weight', 'ASC');