Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a calculated field in a query #36

Closed
bilel opened this issue Dec 27, 2019 · 5 comments
Closed

Create a calculated field in a query #36

bilel opened this issue Dec 27, 2019 · 5 comments

Comments

@bilel
Copy link

bilel commented Dec 27, 2019

Hi,
Is it possible to add Mathematic operations to Queries using this project?
For example, a product of "stock" and "price" and performing that multiplication inside a SUM

We can do this:
$db->product->selectAggregate('SUM', 'price')->get();
which is like : SELECT SUM('price') AS ...

But How to do this? :
SELECT sum(qty*sale_price) as total_stock

@oscarotero
Copy link
Owner

Mmm, it is not possible for now. selectAggregate expects an existing field of the table, not a math operation.
But this could be implemented easily in order to include any expression and store the result in a new field. For example:

$db->product->selectAggregate('SUM', 'qty*sale_price', 'total_stock')->get();

I can work in this feature in one or two weeks. But you feel strong and want to contribute with a PR, you're welcome.

@bilel
Copy link
Author

bilel commented Dec 29, 2019

That already did the job! Thanks
I had to read through those classes to see how it works.

It's a nice code. I'm using it actually as an alternative to REST APIs with small projects.

It started when I didn't found a compiled version of last MariaDB server for ARM distros (Raspberry Pi)... So no Stored Aggregate Functions, no JSON functions...

This one for example was made for a friend for an inventory project.. Live Linking Memento Database app to a LAN mysql server. So he could scan products' barcodes and instantly (Insert/Update) products

Thanks again and sure if something comes to my mind, Ill pull a request.

@oscarotero
Copy link
Owner

Hi. I just made a commit with this change. Could you test it before tag a new release?
Thanks!

@bilel
Copy link
Author

bilel commented Jan 10, 2020

It's working Perfect & Clystar clear! And really Thanks for keeping the same syntax. It would be easier to perform inline calculations this way and that's similar to the usual MySql syntax.

@oscarotero
Copy link
Owner

Great! New version released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants