需要向Codecharge生成的PHP / SQL添加Group By语句

时间:2019-05-15 14:26:23

标签: php mysql codecharge

我试图修改一些早在Codecharge中创建的PHP代码,以在MYSQL语句中包含Group By子句。

我已经测试了我的MYSQL查询,它在phpmyadmin中表现出色。当我将其带入php页面时,它会踢回一个错误,因为WHERE和ORDER子句在不同的位置编码,然后调用查询字符串。显然,当在SQL中保留GROUP BY时,它会中断,因为它是在$ this链中的WHERE之前插入它,因此我需要弄清楚我可以在哪里插入GROUP BY部分代码,以便将它们全部串在一起按照正确的顺序加载页面。以下是将SQL和子句组合在一起的3个部分,并且网页可以正常加载。我按SQL版本注释掉了,因为没有GROUP BY不能正常工作。

//SetOrder Method @5-0FECF370
    function SetOrder($SorterName, $SorterDirection)
    {
        $this->Order = "book_name";
        $this->Order = CCGetOrder($this->Order, $SorterName, $SorterDirection, 
            "");
    }
//End SetOrder Method

//Prepare Method @5-C6449552
    function Prepare()
    {
        $this->wp = new clsSQLParameters($this->ErrorBlock);
        $this->wp->AddParameter("1", "urlshow_id", ccsInteger, "", "", $this->Parameters["urlshow_id"], "", false);
       $this->wp->Criterion[1] = $this->wp->Operation(opEqual, "ttb_books.show_id", $this->wp->GetDBValue("1"), $this->ToSQL($this->wp->GetDBValue("1"), ccsInteger),false);
        $this->Where = $this->wp->Criterion[1];

    }
//End Prepare Method

//Open Method @5-33D1EC54
    function Open()
    {
        $this->CCSEventResult = CCGetEvent($this->CCSEvents, "BeforeBuildSelect");
         $this->CountSQL = "SELECT COUNT(*)  " .
        "FROM (ttb_books INNER JOIN ttb_states ON " .
        "ttb_books.state_id = ttb_states.state_id) INNER JOIN ttb_statuses ON " .
        "ttb_books.status_id = ttb_statuses.status_id";
        $this->SQL = "SELECT *  " .
        "FROM (ttb_books INNER JOIN ttb_states ON " .
        "ttb_books.state_id = ttb_states.state_id) INNER JOIN ttb_statuses ON " .
        "ttb_books.status_id = ttb_statuses.status_id ";

        //$this->SQL = "SELECT ttb_books.book_id, ttb_books.book_name, ttb_books.show_id, ttb_states.state_name, COUNT(ttb_imgs.img_id) AS images " .
        //"FROM (ttb_books INNER JOIN ttb_states ON ttb_books.state_id = ttb_states.state_id) " .
        //"INNER JOIN ttb_statuses ON ttb_books.status_id = ttb_statuses.status_id " .
        //"LEFT JOIN ttb_imgs ON ttb_books.book_id = ttb_imgs.book_id ";
        $this->Group = "ttb_books.book_id, ttb_books.book_name, ttb_books.show_id, ttb_states.state_name";
        $this->CCSEventResult = CCGetEvent($this->CCSEvents, "BeforeExecuteSelect");
        $this->RecordsCount = CCGetDBValue(CCBuildSQL($this->CountSQL, $this->Where, ""), $this);
        $this->query(CCBuildSQL($this->SQL, $this->Where, $this->Order));
        $this->CCSEventResult = CCGetEvent($this->CCSEvents, "AfterExecuteSelect");
        $this->MoveToPage($this->AbsolutePage);
    }
//End Open Method

代码的原始版本仅列出所有书名,并按书名排序。我的版本中还包含一个COUNT,因为我不仅要查看所有书名,而且要查看列表中每本书的页数。就像我说的那样,我的新SQL在phpmyadmin中可以正常工作,而且如果可以使分组正常工作,我相信它也会在页面上显示。谢谢您的帮助!

2 个答案:

答案 0 :(得分:1)

好的,这就是我所做的。我删除了CCBuildSQL行的$ this-> Where部分,并将WHERE直接放入sql语句中,这使我可以使用GROUP BY并获得所需的结果。

    function Open()
{
    $this->CCSEventResult = CCGetEvent($this->CCSEvents, "BeforeBuildSelect");
     $this->CountSQL = "SELECT COUNT(*)  " .
    "FROM (ttb_books INNER JOIN ttb_states ON " .
    "ttb_books.state_id = ttb_states.state_id) INNER JOIN ttb_statuses ON " .
    "ttb_books.status_id = ttb_statuses.status_id";


    $this->SQL = "SELECT ttb_books.book_id, ttb_books.book_name, ttb_books.owned, ttb_books.show_id, ttb_shows.show_name, ttb_states.state_name, COUNT(img_id) AS images " .
    "FROM ttb_books " .
    "INNER JOIN ttb_states ON ttb_books.state_id = ttb_states.state_id " .
    "INNER JOIN ttb_statuses ON ttb_books.status_id = ttb_statuses.status_id " .
    "INNER JOIN ttb_shows ON ttb_books.show_id = ttb_shows.show_id " .
    "LEFT JOIN ttb_imgs ON ttb_books.book_id = ttb_imgs.book_id WHERE ttb_books.show_id=" . $this->Parameters["urlshow_id"] .
    " GROUP BY ttb_books.book_id, ttb_books.book_name, ttb_books.owned, ttb_books.show_id, ttb_shows.show_name, ttb_states.state_name " ;

    $this->CCSEventResult = CCGetEvent($this->CCSEvents, "BeforeExecuteSelect");
    $this->RecordsCount = CCGetDBValue(CCBuildSQL($this->CountSQL, $this->Where, ""), $this);
    $this->query(CCBuildSQL($this->SQL, "", $this->Order));
    $this->CCSEventResult = CCGetEvent($this->CCSEvents, "AfterExecuteSelect");
    $this->MoveToPage($this->AbsolutePage);
}

答案 1 :(得分:0)

您似乎不在IDE中,而是直接更改生成的代码。进入CodeCharge IDE,选择要修改的控件,然后创建一个自定义代码事件,然后将调整后的SQL放在此处。

http://docs.codecharge.com/studio50/html/index.html?http://docs.codecharge.com/studio50/html/ProgrammingTechniques/HowTo/CustomizingDataSource/ModifyOrderBy.html?toc