catfan / Medoo

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

Add: Advanced columns #106

Closed web-monster closed 6 years ago

web-monster commented 10 years ago

Can add extended columns. For example:

$Db->select("test",[
    "[COUNT=*](my_count)",
    "id_category",
    "[SUM=people](my_sum)"
],["is_read"=>0]);
// or
$Db->select("test",[
    "[COUNT(*)](my_count)",
    "id_category",
    "[SUM(people)](my_sum)"
],["is_read"=>0]);
SELECT COUNT(*) AS "my_count","id_category",SUM(people) AS "my_sum" FROM "test" WHERE "is_read" = 0

It would be cool

web-monster commented 10 years ago

Implemented so:

$Db->update("test",["record_count"=>"##record_count-1"],["id"=>1]);
$Db->select("articles",["##COUNT(*) AS count","id_category"],["AND"=>["is_read"=>0,"is_del"=>0]]);

In medoo.php:

public function quote($string)
{
    preg_match('/^\#\#(.*)/', $string, $match);
    if(isset($match[1])) {
        return $match[1];
    }
    return $this->pdo->quote($string);
}

protected function column_quote($string)
{
    preg_match('/^\#\#(.*)/', $string, $match);
    if(isset($match[1])) {
        return $match[1];
    }
    return '"' . str_replace('.', '"."', $string) . '"';
}

Result:

UPDATE "test" SET "record_count" = record_count-1 WHERE "id" = 1
SELECT COUNT(*) AS count,"id_category" FROM "articles" WHERE "is_read" = 0 AND "is_del" = 0
web-monster commented 10 years ago

Will be added? ##

catfan commented 10 years ago

I think it have to make more consideration. The syntax is not friendly enough and is will make it more complex.

web-monster commented 10 years ago

Make you better. Without this functionality very gutted. I can not even make a few COUNT

carlosmellado commented 10 years ago

Can't we do a count(something) column yet?

catfan commented 6 years ago

Check out the new feature raw object https://medoo.in/api/raw.