How do I compare fields in a database query?
You must use where(), like this:

$query = \Drupal::database()-&gt;select('mytable', 't')
  -&gt;addField('t', 'myfield1', 'myfield1')
  -&gt;addField('t', 'myfield2', 'myfield2')
  -&gt;where('t.myfield1 = t.myfield2');