title: SQL: join with OR in condition
body: <p>I have 2 tables:</p>
  
  <pre><code>Devices (id (PK))
  Links (id (PK), device_id_1 (FK), device_id_2 (FK))
  </code></pre>
  
  <p>Which represents devices connected by links.</p>
  
  <p>I need to select all devices connected with a given one (which can be device_id_1 or device_id_2). I tried to do it with the following query:</p>
  
  <pre><code>select d2.*
  from Devices as d1
  left outer join Links as l on d1.id in (l.device_id_1, l.device_id_2)
  left outer join Devices as d2 on d2.id in (l.device_id_1, l.device_id_2)
  where d1.id = 398 and d2.id &lt;&gt; 398;
  </code></pre>
  
  <p>But as soon as I added second <code>JOIN</code> the query returns zero rows. What am I doing wrong?</p>