【发布时间】:2010-04-04 22:46:20
【问题描述】:
我正在尝试在 Kohana 中使用 $this->db 运行一个简单的查询,但是当我尝试在查询中为表使用别名时遇到了一些语法问题:
$result = $this->db
->select("ci.chapter_id, ci.book_id, ci.chapter_heading, ci.chapter_number")
->from("chapter_info ci")
->where(array("ci.chapter_number" => $chapter, "ci.book_id" => $book))
->get();
在我看来,这应该可以正常工作。我是说“chapter_info”应该被称为“ci”,但由于某种原因这并没有被占用。错误非常简单:
There was an SQL error: Table 'gb_data.chapter_info ci' doesn't exist -
SELECT `ci`.`chapter_id`, `ci`.`book_id`, `ci`.`chapter_heading`,
`ci`.`chapter_number`
FROM (`chapter_info ci`)
WHERE `ci`.`chapter_number` = 1
AND `ci`.`book_id` = 1
如果我使用完整的表名而不是别名,我会得到预期的结果而不会出错。这需要我编写更详细的查询,这并不理想。
有没有办法在 Kohana 的查询构建器中为表使用较短的名称?
【问题讨论】:
标签: mysql database syntax kohana