Laravel returns 当参数作为整数传递时,零而不是实际的单元格值
Laravel returns zero instead of the actual cell value when a parameter passed as an integer
我有一个来自 MariaDB 视图的非常简单的查询:
SELECT c.amount, c.discount
FROM factors_view as c
WHERE c.factor_id = 358
当我在 HeidiSQL 中 运行 这个查询时,我得到这个结果:amount = 16000, discount = 1200
但在 Laravel 5.7 原始查询中
$result = \DB::select("
SELECT c.amount,c.discount
FROM factors_view as c
WHERE c.factor_id = 358"
);
结果:amount = 16000, discount = 0
当我将参数放在引号之间时:
$result = \DB::select("
SELECT c.amount,c.discount
FROM factors_view as c
WHERE c.factor_id = '358'"
);
结果:amount = 16000, discount = 1200
c.factor_id 的类型是 int(10) unsigned
。
这对我来说很奇怪;因为区别在于查询条件,而不是选择!
输出是特定列上零值的同一行!
有谁知道发生了什么事吗?
这是我的两个查询的查询日志:
1)
query:"select `c`.`amount`, `c`.`discount` from `factors_view` as `c` where `c`.`factor_id` = ?"
bindings:[0:358]
2)
query:"select `c`.`amount`, `c`.`discount` from `factors_view` as `c` where `c`.`factor_id` = ?"
bindings:[0:"358"]
终于,我找到了这个麻烦错误的原因。为了更正错误,我在视图中更改了 factor_id 的类型:
CAST(factor_id AS UNSIGNED) AS factor_id
它工作正常。
虽然错误已修复,但我仍然没有意识到这种行为。 where 条件必须影响行,而不是单元格的值。这可能是一个 doctrine/dbal 错误?!
请提供SHOW CREATE TABLE
.
我猜测 factor_id
是 VARCHAR
,而它应该是 INT
。
int_col = 123 -- can use index
int_col = '123' -- can use index
char_col = 123 -- 123 dominates; must check all rows, converting char_col to int
char_col = '123' -- can use index
我有一个来自 MariaDB 视图的非常简单的查询:
SELECT c.amount, c.discount
FROM factors_view as c
WHERE c.factor_id = 358
当我在 HeidiSQL 中 运行 这个查询时,我得到这个结果:amount = 16000, discount = 1200
但在 Laravel 5.7 原始查询中
$result = \DB::select("
SELECT c.amount,c.discount
FROM factors_view as c
WHERE c.factor_id = 358"
);
结果:amount = 16000, discount = 0
当我将参数放在引号之间时:
$result = \DB::select("
SELECT c.amount,c.discount
FROM factors_view as c
WHERE c.factor_id = '358'"
);
结果:amount = 16000, discount = 1200
c.factor_id 的类型是 int(10) unsigned
。
这对我来说很奇怪;因为区别在于查询条件,而不是选择!
输出是特定列上零值的同一行!
有谁知道发生了什么事吗?
这是我的两个查询的查询日志:
1)
query:"select `c`.`amount`, `c`.`discount` from `factors_view` as `c` where `c`.`factor_id` = ?"
bindings:[0:358]
2)
query:"select `c`.`amount`, `c`.`discount` from `factors_view` as `c` where `c`.`factor_id` = ?"
bindings:[0:"358"]
终于,我找到了这个麻烦错误的原因。为了更正错误,我在视图中更改了 factor_id 的类型:
CAST(factor_id AS UNSIGNED) AS factor_id
它工作正常。
虽然错误已修复,但我仍然没有意识到这种行为。 where 条件必须影响行,而不是单元格的值。这可能是一个 doctrine/dbal 错误?!
请提供SHOW CREATE TABLE
.
我猜测 factor_id
是 VARCHAR
,而它应该是 INT
。
int_col = 123 -- can use index
int_col = '123' -- can use index
char_col = 123 -- 123 dominates; must check all rows, converting char_col to int
char_col = '123' -- can use index