========================================================== SELECT nid, title FROM {node} n LEFT JOIN {node_revisions} r ON n.nid = r.nid WHERE n.uid = %d AND n.type = '%s' db_select('node', 'n') ->fields('n', array('nid', 'title')) ->leftJoin('node_revisions', 'r', ' ON n.nid = r.nid') ->condition('n.uid', ) ->condition('n.type', ) ->execute(); - remove ' ON' from join - add placeholders for conditions ========================================================== SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 OFFSET 0 Parse error: Unexpected clause on line 1 SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 OFFSET 0 ^ found: "OFFSET" - MySQL docs say: For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax. - so is OFFSET not ANSI SQL? ========================================================== SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 db_select('users', 'u') ->fields('u', array('uid', 'name', 'status', 'created', 'access')) ->condition('uid', 0, '<>') ->range(0, 50) ->execute(); - is the table alias required in the condition? No. - Should we include it if the table has an alias? ========================================================== SELECT uid, name, status, created, access FROM {users} u WHERE uid <> 0 LIMIT 50 OFFSET 8 db_select('users', 'u') ->fields('u', array('uid', 'name', 'status', 'created', 'access')) ->condition('uid', 0, '<>') ->range(8, 50) ->execute(); ========================================================== SELECT FROM {mytable} WHERE (a = 1 AND b = 'foo' OR (c = 'bar')) Parse error: Expected columns or a set function on line 1 SELECT FROM {mytable} WHERE (a = 1 AND b = 'foo' OR (c = 'bar')) ^ found: "FROM" ========================================================== SELECT * FROM {mytable} WHERE (a = 1 AND b = 'foo' OR (c = 'bar')) db_select('mytable', 'm') ->fields() ->condition(db_and() ->condition('a', 1) ->condition('b', 'foo') ) ->execute(); - missing the OR condition - ->condition(db_or()->condition('c', 'bar')) - is this correct? - see block.module line 380 - looks like WHERE (module = $module AND delta = $delta) OR theme = $theme - constructed in left to right order $condition = db_and() ->condition('module', $module) ->condition('delta', $delta); $or->condition($condition); ========================================================== SELECT * FROM {mytable} WHERE (a = 1 AND b = 'foo' OR (c = 'bar')) - is the following like what we are trying to output? $and = db_and()->condition('a', 1)->condition('b', 'foo'); $or = db_or()->condition($and)->condition('c', 'bar'); db_select('mytable', 'm') ->fields() ->condition($or) ->execute();