tags:

views:

33

answers:

1

Please consider the following table 'mmm':

select * from mmm;

Output:

+-------+-------+------+  
| texto | value | n    |  
+-------+-------+------+  
| aaa   |    10 | 1    |  
| aab   |    10 | 1    |  
| aaa   |    11 | 1    |  
| aab   |    11 | 1    |  
| aaa   |    10 | 2    |  
+-------+-------+------+  

The command:

select a.*, '--', b.* 
  from mmm a 
left join mmm b on (a.n=b.n) 
where  a.value < b.value 
  and a.texto ='aaa' 
  and b.texto='aab';

returns:

+-------+-------+------+----+-------+-------+------+  
| texto | value | n    | -- | texto | value | n    |  
+-------+-------+------+----+-------+-------+------+  
| aaa   |    10 | 1    | -- | aab   |    11 | 1    |  
+-------+-------+------+----+-------+-------+------+  

That's fine. But what I want is something like:

+-------+-------+------+----+-------+-------+------+  
| texto | value | n    | -- | texto | value | n    |  
+-------+-------+------+----+-------+-------+------+  
| aaa   |    10 | 1    | -- | aab   |    11 | 1    |  
+-------+-------+------+----+-------+-------+------+  
| aaa   |    10 | 2    | -- | NULL  |  NULL | NULL |  
+-------+-------+------+----+-------+-------+------+  
+4  A: 
select a.*, '--', b.* 
  from mmm a 
left join mmm b on (a.n=b.n) 
where  (a.value < b.value or b.value is null)
  and a.texto ='aaa' 
  and (b.texto='aab' or b.textto is null);

or:

select a.*, '--', b.* 
  from mmm a 
left join mmm b on (a.n=b.n and a.value < b.value and b.texto = 'aab') 
where a.texto ='aaa' ;
Martin
+1: The latter one was the version I was going to post. But there's no need for the brackets around the LEFT JOIN criteria
OMG Ponies
Thank you Martin. This was a smaller version of a big (at least for me) sql query. The problem was that I was inserting some conditions about the second table on the _where_ clause instead of the _on_ clause of the _left join_.
Luis