Given the query fragment:

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

I need to add a condition like this:

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

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

$query->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?

1 Answer 1


You must use where(), like this:

$query = \Drupal::database()->select('mytable', 't')
  ->addField('t', 'myfield1', 'myfield1')
  ->addField('t', 'myfield2', 'myfield2')
  ->where('t.myfield1 = t.myfield2');
  • Thank you! That worked! I could not find that on the documentation - at least not where I looked. :-/ Commented Dec 18, 2019 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.