如何在 Kohana 3 中将表名作为参数传递给数据库查询
我有一个复杂的 3 层查询,我想将表名称作为参数传递。 我使用的代码像这样:
DB::query(Database::SELECT, 'SELECT
SUM(volume) AS `volumeTotal`,
MIN(volume) AS `volumeMin`,
MAX(volume) AS `volumeMax`,
AVG(volume) AS `volumeAverage`,
STD(volume) AS `volumeSD`
FROM (
SELECT TIMEDIFF (DT,CAST( @dt AS DATETIME )) AS Ival, CAST( @dt AS DATETIME ) AS `start` , @dt := DT AS `stop` , `volume`
FROM ( SELECT @dt := NULL) dt,
( SELECT * FROM :table WHERE id = :XID) vals
ORDER BY dt ASC) vI')
->param(':table', $tableName)
->param(':XID', $ID)
->execute();
它不适合我为此使用查询生成器(而且我认为这不是最佳方式)。 当然,我可能会像“.$tableName.
”一样直接在查询中输入参数,但我认为应该有更正确的方法。
问题是:
如何使用 Kohana 将表名作为参数传递给数据库查询?
I have a complex 3 layer query in wich I`d liked to pass table name as parameter.
I use code like this:
DB::query(Database::SELECT, 'SELECT
SUM(volume) AS `volumeTotal`,
MIN(volume) AS `volumeMin`,
MAX(volume) AS `volumeMax`,
AVG(volume) AS `volumeAverage`,
STD(volume) AS `volumeSD`
FROM (
SELECT TIMEDIFF (DT,CAST( @dt AS DATETIME )) AS Ival, CAST( @dt AS DATETIME ) AS `start` , @dt := DT AS `stop` , `volume`
FROM ( SELECT @dt := NULL) dt,
( SELECT * FROM :table WHERE id = :XID) vals
ORDER BY dt ASC) vI')
->param(':table', $tableName)
->param(':XID', $ID)
->execute();
It is not suitable for me to use query builder for this (and I think it is not optimal way).
Of course, I may input parameters into query directly like ".$tableName.
", but I think there should be a more correct way.
The question is:
How to pass table name as parameter to database query using Kohana?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要使用
DB::expr($tableName)
而不是$tableName
。Need to use
DB::expr($tableName)
instead of$tableName
.