在Yii 2中执行查询时发生一般错误SQLSTATE[HY000]
这是我的密码:在Yii 2中执行查询时发生一般错误SQLSTATE[HY000],yii,yii2,Yii,Yii2,这是我的密码: $sql = " SET @run_balqty :=0; SELECT transaction_date, item_id, item_description, unit_id, quantity, ( @run_balqty := @run_balqty + quantity ) AS ba
$sql = "
SET @run_balqty :=0;
SELECT
transaction_date,
item_id,
item_description,
unit_id,
quantity,
( @run_balqty := @run_balqty + quantity ) AS balance_qty,
reference_code
FROM
`report_ledger` AS ledger
WHERE
item_id = 3147
ORDER BY
transaction_date ";
$query = Yii::$app->db->createCommand($sql)->queryAll();
当我试图运行此代码时。我得到这个错误
SQLSTATE[HY000]:一般错误
现在。。我的问题是:为什么我会出现这个错误?我如何让它运行?
我需要帮助。谢谢。您正在尝试获取一个查询的结果,该查询包含一个命令(
SET@run\u balqty:=0
),该命令不可“获取”。您必须首先单独执行该命令,然后可以调用queryal()
来执行SELECT
query命令:
Yii::$app->db->createCommand("SET @run_balqty :=null;")->execute();
$sql = "
SELECT
transaction_date,
item_id,
item_description,
unit_id,
quantity,
( @run_balqty := @run_balqty + quantity ) AS balance_qty,
reference_code
FROM
`report_ledger` AS ledger
WHERE
item_id = 3147
ORDER BY
transaction_date ";
$query = Yii::$app->db->createCommand($sql)->queryAll();
注意:小心使用SET
语句,请阅读