查询1:
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet,
SUM(apple_mail_int) AS Apple_Mail,
SUM(blackberry_int) AS Blackberry,
SUM(Eudora_int) AS Eudora,
SUM(gmail_int) AS Gmail,
SUM(hotmail_int) AS Hotmail,
SUM(lotus_notes_int) AS Lotus_Notes,
SUM(other_int) AS Other,
SUM(other_webmail_int) as Other_Web_Mail,
SUM(Outlook_int) AS Outlook,
SUM(Postbox_int) AS Postbox,
SUM(sparrow_int) AS Sparrow,
SUM(thunderbird_int) AS Thunderbird,
SUM(windowsLiveMail_int) AS Windows_Live_Mail,
SUM(yahoo_int) AS Yahoo,
SUM(iPad_int) AS iPad,
SUM(iPhone_int) AS iPhone,
SUM(iPod_int) AS iPod
FROM mytable;
Query 2:
select sum(aol_int + android_phone_int + androidtablet_int+apple_mail_int+blackberry_int+Eudora_int+gmail_int+hotmail_int+lotus_notes_int+other_int+other_webmail_int+Outlook_int+Postbox_int+sparrow_int+thunderbird_int+windowsLiveMail_int+yahoo_int+iPad_int+iPhone_int+iPod_int)
as total_percentage
FROM mytable;
当我总结查询1的结果时,与通过Query2得到的数据相比,我获得的总和不同。 Query2中的值小于查询1.为什么会这样?
故障:
我试着写这样的查询:
SELECT SUM( ISNULL(aol_int,0) +
ISNULL(android_phone_int,0) +
ISNULL(androidtablet_int,0) +
ISNULL(apple_mail_int,0) +
ISNULL(blackberry_int,0) +
ISNULL(Eudora_int,0) +
ISNULL(gmail_int,0) +
ISNULL(hotmail_int,0) +
ISNULL(lotus_notes_int,0) +
ISNULL(other_int,0) +
ISNULL(other_webmail_int,0) +
ISNULL(Outlook_int,0) +
ISNULL(Postbox_int,0) +
ISNULL(sparrow_int,0) +
ISNULL(thunderbird_int,0) +
ISNULL(windowsLiveMail_int,0)+
ISNULL(yahoo_int,0) +
ISNULL(iPad_int,0) +
ISNULL(iPhone_int,0) +
ISNULL(iPod_int,0) )AS total_percentage
FROM mytable;
但是,我在MySQL工作台上运行上述查询后出现错误:
错误代码:1582。调用本机函数“ISNULL”时参数计数不正确。这有什么不对?
答案 0 :(得分:2)
如果某些行中的某些列包含null
,则可能会发生这种情况。发生这种情况时,即使只有一个null
列的行中的所有列都会在添加链中产生null
,因此该行将不会添加任何内容。
以下是此效果的简短演示。设置:
create table test(x int null, y int null);
insert into test(x,y) values (1,null);
insert into test(x,y) values (null,2);
insert into test(x,y) values (3,3);
查询:
select sum(x+y) from test; -- Shows 6
select sum(x)+sum(y) from test -- Shows 9
在sqlfiddle上查看此演示:link。