catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

How to select * and a function like count #926

Closed lcsqlpete closed 3 years ago

lcsqlpete commented 4 years ago

I need to use select() to select all columns plus a count() function from a table. I specified the column names as

["*", "PanelCount" => Medoo::raw("count(CurtainEventID)")]

This resulted in the following error:

Uncaught InvalidArgumentException: Incorrect column name "" in /www/Dev-KleenEdge/public_html/vendor/catfan/medoo/src/Medoo.php:526

Specifying "*" as the columns parameter works OK and specifying ["PanelCount" => Medoo::raw("count(CurtainEventID)")] also works but both of them together throw the above error.

Does this mean I can't select all columns and a count in the same select() or is there some other way to do this? I really hope there is because if I have to drop back to using the query() method for this, using Medoo is pointless for me.

catfan commented 4 years ago

It's not recommend to use * for fetching all columns in query. Just specify the detail column name as possible.

What if one of column also named PanelCount? Should it output as data from database, or as data from count(xxxx)? Will be conflicted.

lcsqlpete commented 4 years ago

I know * is not recommended but that is a lot different than it causing a fatal error.

There is not column named PanelCount in the table and even if there was, it would simply cause an SQL error to be thrown.

I need all the columns in the table which is why I use *. Requiring me to individually specify all 23 column names is totally unnecessary.

Medoo should never prevent a user from constructing and executing a valid SQL statement.