0

我正在尝试修改一些很久以前在 Codecharge 中创建的 PHP 代码,以在 MYSQL 语句中包含 Group By 子句。

我已经测试了我的 MYSQL 查询,它在 phpmyadmin 中运行良好。当我将它带入 php 页面时,它会返回一个错误,因为 WHERE 和 ORDER 子句编码在不同的位置,然后调用查询字符串。显然,当 GROUP BY 留在 SQL 中时,它会中断,因为它是在 $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 中运行良好,如果我能让 Grouping 也能正常工作,我相信它也会出现在页面上。谢谢您的帮助!

4

2 回答 2

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);
}
于 2019-05-16T13:01:53.037 回答
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

于 2019-05-15T19:47:31.487 回答