我正在试图弄清楚为什么这个查询SELECT很好,但是我不能用它来创建一个临时表... IF = '', 0
这些东西以前不存在于原始查询中所以我添加它是为了确保我没有尝试总结任何可能是空字符串的东西,但它没有解决问题。此选择有效并返回正确的结果集:
SELECT
SUM(((IF(reserve_transactions.Revenue = '',
0,
reserve_transactions.Revenue) * IF(reserve_transactions.Confidence = '',
0,
reserve_transactions.Confidence)) / 100)) AS rawadjusted
FROM
(store_locations
LEFT JOIN reserve_transactions ON ((store_locations.ID = location_sales)))
GROUP BY store_locations.Loc_Long
但这不是:
CREATE TEMPORARY TABLE tmptable_which_doesnt_work AS SELECT
SUM(((IF(reserve_transactions.Revenue = '',
0,
reserve_transactions.Revenue) * IF(reserve_transactions.Confidence = '',
0,
reserve_transactions.Confidence)) / 100)) AS rawadjusted
FROM
(store_locations
LEFT JOIN reserve_transactions ON ((store_locations.ID = location_sales)))
GROUP BY store_locations.Loc_Long
错误是:Error Code: 1292. Truncated incorrect DECIMAL value: ''
实际上我还有其他问题,情况是一样的。有时与其他截断不正确的类型。我在这里缺少什么?
答案 0 :(得分:0)
我认为问题可能是数据和仅选择数字条目的逻辑 - 您可以尝试按如下方式定义函数:
CREATE FUNCTION IsNumeric (sIn varchar(1024))
RETURNS tinyint
RETURN sIn REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
并将其应用于两个参数的where子句中,最终修剪不必要的空格(根据How do I check to see if a value is an integer in MySQL?)