how to use if exists inside db_query I try to create a condition that checks if user has already created a node. 

I use this code which returns the last created node by the user. Adding simple if statement I can return true/false.

$xxx = db_query("SELECT nid FROM {node} WHERE (type = :type1) AND (uid = '123')", array("type1" =&gt; 'nodetype_x'))-&gt;fetchField();
return $xxx;


I tried to do it more efficient to check only for first result like this.

$xxx = db_query("
IF EXISTS(
      SELECT nid FROM {node} WHERE (type = :type1) AND (uid = '123')
)
SELECT 1
ELSE
SELECT 0", array("type1" =&gt; 'nodetype_x'))-&gt;fetchField();


But get syntax error. What would be the correct syntax? Thank you
This is really not the ideal way to do this. Instead you may want to try something like:

$query = db_select('node', 'n')
  -&gt;condition('n.type', $type)
  -&gt;condition('n.uid', $uid)
  -&gt;fields('n', array('nid'));

$count = $query-&gt;countQuery()-&gt;execute()-&gt;fetchField();

if ($count) {
  return true;
}

return false;