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

Get greatest Value #1024

Open
JuliusFroglet opened this issue Dec 10, 2023 · 1 comment
Open

Get greatest Value #1024

JuliusFroglet opened this issue Dec 10, 2023 · 1 comment

Comments

@JuliusFroglet
Copy link

Hi,
how do I get the row with a columns greatest value?

I thoght it could work like this, but unfotunately... no effect.

$db->get($tables["dates"],"greatest(start)");

this also won't work

$db->get($tables["dates"],"max(start)");

Any ideas, who to get this running?

@huseyinaslim
Copy link
Contributor

Hi @JuliusFroglet ,

The library's get function is designed to list multiple lines, and upon examination, you can see that the second parameter of this function is used to modify the LIMIT statement in order to limit the number of returned results.

    /**
     * A convenient SELECT * function.
     *
     * @param string       $tableName The name of the database table to work with.
     * @param int|array    $numRows   Array to define SQL limit in format Array ($offset, $count)
     *                                or only $count
     * @param string|array $columns   Desired columns
     *
     * @return array|MysqliDb Contains the returned rows from the select query.
     * @throws Exception
     */
    public function get($tableName, $numRows = null, $columns = '*')

In your code, however, the second parameter is used to indicate the column name. This is an incorrect usage. If we want to filter columns with the get function, you should demonstrate a usage like this:

We should use it in this way;

$db->get($tables["dates"], null, "greatest(start)");  

$db->get($tables["dates"], null, "max(start)");  

However, functions like GREATEST, MAX return only a single row result, so using getOne instead of get may be more ideal. In fact, if you want to obtain only a single column, using getValue is the most ideal.

I recommended it for example;

$db->getOne($tables["dates"], "id, greatest(start)");  
$db->getValue($tables["dates"], "greatest(start)");  

$db->getOne($tables["dates"], "id, max(start)"); 
$db->getValue($tables["dates"], "greatest(start)");  

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