How do I compare fields in a database query? Given the query fragment:

$query = $connection-&gt;select('mytable', 't');
$query-&gt;addField('t', 'myfield1', 'myfield1');
$query-&gt;addField('t', 'myfield2', 'myfield2');


I need to add a condition like this:

$query-&gt;condition('t.myfield1', 't.myfield2', '=');


But it doesn't seem to work. For comparison, I can use:

$query-&gt;condition('t.myfield1', 'banana', '=');


... and that works. But it seems comparing two fields to see if they match does not work? I can write this out in straight SQL code and properly query the database in a console, but can't duplicate in Drupal's database API.

What am I doing wrong?
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');