gpt4 book ai didi

mysql - 在 Kohana 查询中使用表别名?

转载 作者:可可西里 更新时间:2023-11-01 07:29:23 24 4
gpt4 key购买 nike

我试图在 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 的查询构建器中为表使用较短的名称吗?

最佳答案

在 Kohana 3 中它就足够了:

->from( array('table_name', 'alias') )

这将创建包含以下内容的查询:

FROM 'table_name' AS 'alias'

我已经测试过它并且有效。祝你好运。

关于mysql - 在 Kohana 查询中使用表别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2576159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com