Well I’ve been able to answer my own question, and the possibility is amazing!
Basically you can define a “view” in MySQL with as many aggregates and additional columns as you choose, and that View acts like another table… so the view appears as another table in the Database Query action and can be selected from there.
The other thing I’ve discovered is the use of the CASE statement within a select which means I don’t really need to use a stored function.
So here is a simple view example.
We have a contacts table:
first_name last_name type_code
=====================================
Charlie Cole c
Sally Smith s
Referencing a contact_type table
type_code type_name
=====================
c Client
s Suppler
e Employee
Then we can create a view which does all the joins and aggregate columns that we need like this:
CREATE VIEW view_contacts AS
SELECT
contacts.id, contacts.first_name, contacts.last_name, contacts.type_code,
CASE
WHEN LENGTH(contacts.type_code)>0 THEN contact_types.type_name
ELSE "Unspecified"
END AS type_name
FROM contacts
LEFT JOIN contact_types ON (contact_types.type_code=contacts.type_code)
WHERE
contacts.is_deleted=0;
Having defined this in the database, the table view_contacts
is now available to choose in a Database Query and if required to be filtered in the Conditions tab by any of the fields that have been selected in the view.
THIS CAN MAKE A MASSIVE DIFFERENCE TO PERFORMANCE ON THE CLIENT SIDE!
As previous to realising this, I would resolve the display of type_name
on the client side using a .where() statement which is very memory intensive!