How to add and condition in Query I have to fetch nodes where it doesn't have both roles(6,7) in it,at the same time  exactly.

I don't want to show role 7 nodes, but it some times have role 6 attached to it also ,at that time i want to omit those nodes. 

This is the query which i wrote for that 

$nids_result = db_select('node', 'n');
$nids_result-&gt;join('users','u','n.uid = u.uid');
$nids_result-&gt;join('users_roles','ur','u.uid = ur.uid');
$nids_result-&gt;fields('n', array('nid'));
$nids_result-&gt;condition('ur.rid', 7, '&lt;&gt;');
$nids_result = $nids_result-&gt;execute()-&gt;fetchAll();


But this is fetching me all nodes including role 7 also,because role 6 is also some times available in the nodes. 

How do I write and condition where it checks for both 6 and 7 roles are available and omit those nodes.Thanks in advance.
By default it is and condition

$nids_result = db_select('node', 'n');
$nids_result-&gt;join('users','u','n.uid = u.uid');
$nids_result-&gt;join('users_roles','ur','u.uid = ur.uid');
$nids_result-&gt;fields('n', array('nid'));
$nids_result-&gt;condition('ur.rid', 7, '&lt;&gt;');
$nids_result-&gt;condition('ur.rid', 6, '&lt;&gt;');

// if you have devel installed check this.
dpq($nids_result);
$nids_result = $nids_result-&gt;execute()-&gt;fetchAll();


debugging the query with dpq will tell yuo the exact query that you can run on phpmyadmin and can see what you are missing.

Updated:

if you need query like

SELECT n.nid AS nid
FROM 
{node} n
INNER JOIN {users} u ON n.uid = u.uid
INNER JOIN {users_roles} ur ON u.uid = ur.uid
WHERE ( (ur.rid &lt;&gt; :db_condition_placeholder_0) OR( (ur.rid &lt;&gt; :db_condition_placeholder_1) AND (ur.rid &lt;&gt; :db_condition_placeholder_2) ))


Code will be :

   $nids_result = db_select('node', 'n');
   $nids_result-&gt;join('users','u','n.uid = u.uid');
   $nids_result-&gt;join('users_roles','ur','u.uid = ur.uid');
   $nids_result-&gt;fields('n', array('nid'));
   $db_or = db_or();
   $db_or-&gt;condition('ur.rid', 7, '&lt;&gt;');
   $db_or-&gt;condition(db_and()-&gt;condition('ur.rid', 7, '&lt;&gt;')-&gt;condition('ur.rid', 6, '&lt;&gt;'));
   $nids_result-&gt;condition($db_or);
   $src = $nids_result-&gt;__toString();
   dpm($src);