4

Is it possible to compare two database fields in the query api? For example I want compare the fields tstamp and crdate like:

SELECT * FROM tt_content WHERE tstamp > crdate;

In the query api I could not found a solution. To get all records and compare the fields in a loop is not a performed way, because this could be over 2 million records (in my real case).

Thanks for your help.

chriwo
  • 51
  • 3

1 Answers1

4

The only way I can think of (and that the query builder supports) is to directly supply the statement. It'll look like this:

$query = $contentElementRepository->createQuery();
$query->statement('SELECT * FROM tt_content WHERE tstamp > crdate');
$matchingContentElements = $query->execute();

This probably breaks the database abstraction layer, so use it with caution. statement() has a second parameter where you can put parameters, in case you need some user input in the query.

Maybe there is another way to do this which I don't know, I'd be really interested in it myself.

Jost
  • 5,948
  • 8
  • 42
  • 72