Ari*_*tle 4 mysql database syntax kohana
我正在尝试$this->db在Kohana中运行一个简单的查询,但是当我尝试在查询中使用表的别名时遇到了一些语法问题:
$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();
Run Code Online (Sandbox Code Playgroud)
在我看来,这应该工作得很好.我说"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
Run Code Online (Sandbox Code Playgroud)
如果我使用完整的表名而不是别名,我会得到预期的结果而不会出错.这需要我写更详细的查询,这是不理想的.
有没有办法在Kohana的查询构建器中为表使用较短的名称?
在Kohana 3中就足够了:
->from( array('table_name', 'alias') )
Run Code Online (Sandbox Code Playgroud)
这将创建包含以下内容的查询:
FROM 'table_name' AS 'alias'
Run Code Online (Sandbox Code Playgroud)
我测试了它,它的工作原理.祝好运.
| 归档时间: |
|
| 查看次数: |
3022 次 |
| 最近记录: |