In optimizing one of the subqueries, I got to this state:
SELECT foo.id FROM foo INNER JOIN bar AS f ON foo.id = bar.foo_id GROUP BY foo.id HAVING MAX(foo.baz) != MAX(bar.baz) The following is important here:
- used maridb
- tables foo and bar are related one-to-many
- subqueries are not allowed
- returns one field - foo.id
I don’t like the hak with an aggregate function in HAVING - if it makes sense for bar.baz field (select only those records for which the foo.baz field is not equal to the maximum bar.baz), then in the case of the foo.baz field it is used exclusively in order to remove the field from the SELECT block. Plus, there is a risk that max (*) does not return the expected result.
How to improve the query?