我的一个MySQL查询在Zend 1.11.12中运行完全正常,但在Zend 1.12.8中运行不正常。 查询 -
$query = $this->select()
->setIntegrityCheck(false)
->from(
array('a' => 'tbcc_reports'),
array('report_id', 'report_code', 'report_heading',
'report_description', 'report_page_name',
'report_published_date', 'price_policy_id',
'timestampdiff(month,report_published_date,now()) as interval')
)
->join(
array('b' => 'tbcc_price_policy_master'),
'a.price_policy_id = b.price_policy_id',
array(
'pr_price as price',
'CAST(reportPriceCalculator(TIMESTAMPDIFF(MONTH,a.report_published_date,NOW()), pr_price) AS UNSIGNED) AS realprice'
)
)
->join(
array('c' => 'tbcc_authors'),
'a.primary_auth_id = c.author_id',
array('author_fname', 'author_lname')
)
->where("(a.report_cat_id = $catId) AND a.report_status='1' AND a.report_parent_id=0")
->order($order);
我搜索了您的论坛并尝试了列出的可能解决方案,但没有成功。 当我打印查询时,它就像这样 -
SELECT `a`.`report_id`, `a`.`report_code`, `a`.`report_heading`,
`a`.`report_description`, `a`.`report_page_name`, `a`.`report_published_date`,
`a`.`timestampdiff(month,report_published_date,now())` AS `interval`,
`a`.`price_policy_id`, `b`.`pr_price` AS `price`,
`CAST(``reportPriceCalculator``(TIMESTAMPDIFF(MONTH,report_published_date,NOW()), ``b```.```pr_price``) AS UNSIGNED)` AS `realprice`,
`c`.`author_fname`, `c`.`author_lname`
FROM `tbcc_reports` AS `a`
INNER JOIN `tbcc_price_policy_master` AS `b` ON a.price_policy_id = b.price_policy_id
INNER JOIN `tbcc_authors` AS `c` ON a.primary_auth_id = c.author_id
WHERE ((a.report_cat_id = 1) AND a.report_status='1' AND a.report_parent_id=0)
ORDER BY `a`.`report_published_date` DESC
如果我将查询更改为 -
SELECT `a`.`report_id`, `a`.`report_code`, `a`.`report_heading`,
`a`.`report_description`, `a`.`report_page_name`, `a`.`report_published_date`,
timestampdiff(month,a.report_published_date,now()) AS `interval`,
`a`.`price_policy_id`, `b`.`pr_price` AS `price`,
CAST(reportPriceCalculator(TIMESTAMPDIFF(MONTH,a.report_published_date,NOW()), b.pr_price) AS UNSIGNED) AS `realprice`,
`c`.`author_fname`, `c`.`author_lname`
FROM `tbcc_reports` AS `a`
INNER JOIN `tbcc_price_policy_master` AS `b` ON a.price_policy_id = b.price_policy_id
INNER JOIN `tbcc_authors` AS `c` ON a.primary_auth_id = c.author_id
WHERE ((a.report_cat_id = 2) AND a.report_status='1' AND a.report_parent_id=0)
ORDER BY `report_published_date` DESC
它运行正常并列出详细信息。我无法在基于Zend的查询中实现上述查询。 你能看一下吗?
答案 0 :(得分:0)
好吧,关于你的代码/问题的一些事情:
1-编写干净的代码是轻松编码调试的第一步 2-请花点时间写一个" clear"问题:您可以轻松查看已发布的问题与现在的问题之间的区别:这会更加关注您的问题 3-通过正确转义输入(或使用占位符来注意SQL注入:请参阅下面的方法和where方法中的原始方法)
您的查询中的问题是数据库适配器正在尝试在您的查询中引用标识符。如您所见:您的reportPriceCalculator
成为''reportPriceCalculator''
。要阻止在查询中添加任何引号,请使用Zend_Db_Expr
。
您的查询应该变为:
$query = $this->select()
->setIntegrityCheck(false)
->from(
array('a' => 'tbcc_reports'),
array('report_id', 'report_code', 'report_heading',
'report_description', 'report_page_name',
'report_published_date', 'price_policy_id',
'timestampdiff(month,report_published_date,now()) as interval')
)
->join(
array('b' => 'tbcc_price_policy_master'),
'a.price_policy_id = b.price_policy_id',
array(
'price' => 'pr_price',
'realprice' => new Zend_Db_Expr(
'CAST(reportPriceCalculator(TIMESTAMPDIFF('
. 'MONTH,a.report_published_date,NOW()), pr_price)'
. ' AS UNSIGNED)'
)
)
)
->join(
array('c' => 'tbcc_authors'),
'a.primary_auth_id = c.author_id',
array('author_fname', 'author_lname')
)
->where('a.report_cat_id = ?', $catId)
->where('a.report_status = 1')
->where('a.report_parent_id = 0')
->order($order);
希望有所帮助