我正在尝试修改一些很久以前在 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 也能正常工作,我相信它也会出现在页面上。谢谢您的帮助!