Skip to content

Add EXCEPT and EXCEPT ALL set operations to Query Builder #19364

@dereuromark

Description

@dereuromark

After #19360 I did a deep dive into other missing functionality
Some are postgres only, others are also not supported across multiple DBs.
The following seems to have enough viable traction to support IMO

Summary

Add support for EXCEPT and EXCEPT ALL set operations to complement the existing UNION, UNION ALL, INTERSECT, and INTERSECT ALL methods.

Database Support

Operation PostgreSQL MySQL SQLite SQL Server
EXCEPT ✅ (8.0.31+)
EXCEPT ALL ✅ (3.39+)

Proposed API

$query1 = $this->Users->find()->select(['email']);
$query2 = $this->Admins->find()->select(['email']);

// Users that are not admins
$query1->except($query2);

// With duplicates preserved
$query1->exceptAll($query2);

Notes

  • EXCEPT has broad support across all major databases
  • EXCEPT ALL is not supported by MySQL - could either throw an exception or document the limitation
  • Implementation should follow the same pattern as existing union(), unionAll(), intersect(), intersectAll() methods

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions