这个问题已在本网站上被问过几次,但是这些解决方案都没有对我有用。
我在查询中运行查询:
//this query is to count variables
$sql = "SELECT variabletype, COUNT(variabletype) AS value_occurrence FROM variable GROUP BY variabletype ORDER BY value_occurrence DESC";
$vars_query = mysqli_query($con,$sql) or die(mysqli_error($con));
$pos = array();
while($vars = mysqli_fetch_array($vars_query, MYSQLI_ASSOC))
{
//each variable has many different values, some positive, some negative, in this query I am trying to understand of the values found, how many of them are positive and pushing the result into an array.
$times_positive_qry = mysqli_query($con, "SELECT variable.variabletype, COUNT(value.valueid) AS positive_occurrence FROM variable INNER JOIN value On variable.variableid=value.variableid WHERE variable.variabletype = '" .$vars['variabletype']. "' AND value.valuelift>0.00 AND value.valuesignificant=1 GROUP BY variable.variabletype ORDER BY positive_occurrence DESC");
$times_positive = mysqli_fetch_array($times_positive_qry, MYSQLI_ASSOC);
$pos[] = $times_positive['positive_occurrence'];
}
我的结果是:
var s1 = ["10","3","2","3","4","5","3","3","2","1",null,"2","2",null,"4","3","3","2","2","2","1","1","1",null,"1",null,null,null,"1","1",null,"1","1",null,"3",null,"1","1","1",null]
您会注意到某些结果以 null 的形式返回,这意味着特定变量没有正值。无论如何,我可以用0&替换空值?我已尝试IFNULL
和COALESCE
,但他们没有工作。
最终我需要空值为0,因为我正在绘制数组图形,如果我有空值,图形将不会显示。
有什么想法吗?
答案 0 :(得分:1)
使用IFNULL
功能,如
IFNULL(your_column,0)
根据您的评论,您错误地执行了coalesce
。它应该像下面一样。原因
如果valueid
为null
,那么COUNT(value.valueid)
无论如何都会为0(尝试将样本视为select count(null) as total
)。
SELECT IFNULL(variable.variabletype,0), <-- Change Here
COUNT(value.valueid) AS positive_occurrence
FROM variable
INNER JOIN value
On
variable.variableid=value.variableid
WHERE variable.variabletype = '" .$vars['variabletype']. "'
AND
value.valuelift>0.00
AND
value.valuesignificant=1
GROUP BY variable.variabletype
ORDER BY positive_occurrence DESC
答案 1 :(得分:0)
而不是
"SELECT variable.variabletype, COUNT(value.valueid)..."
"SELECT variable.variabletype, isnull(COUNT(value.valueid),0)"
答案 2 :(得分:0)
答案 3 :(得分:0)
你可以使用ISNULL()函数
ISNULL(COUNT(value.valueid),0)
答案 4 :(得分:0)
如果你想要你可以手动浏览你的php数组并进行字符串替换,或者你可以访问数据库,你可以做类似的事情
"update variable set value=0 where value is NULL;"
答案 5 :(得分:0)
真正的问题在于第二个问题:
$vars_query = mysqli_query($con,$sql) or die(mysqli_error($con));
$pos = array();
while($vars = mysqli_fetch_array($vars_query, MYSQLI_ASSOC)) {
//each variable has many different values, some positive, some negative, in this query I am trying to understand of the values found, how many of them are positive and pushing the result into an array.
$times_positive_qry = mysqli_query($con, "SELECT variable.variabletype, COUNT(value.valueid) AS positive_occurrence FROM variable INNER JOIN value On variable.variableid=value.variableid WHERE variable.variabletype = '" .$vars['variabletype']. "' AND value.valuelift>0.00 AND value.valuesignificant=1 GROUP BY variable.variabletype ORDER BY positive_occurrence DESC"); $times_positive = mysqli_fetch_array($times_positive_qry, MYSQLI_ASSOC);
$pos[] = $times_positive['positive_occurrence'];
}
问题是您的WHERE
子句可以消除所有结果。这意味着整个结果集可以为null。 您将获得零行的结果集。您需要使用LEFT JOIN
来确保始终获得一行。
$times_positive_qry
的查询字符串应如下所示:
"SELECT variable.variabletype,
COUNT(value.valueid) AS positive_occurrence
FROM variable
LEFT JOIN value
ON variable.variableid = value.variableid
WHERE variable.variabletype = '" .$vars['variabletype']. "'
AND ((value.valuelift > 0.00 AND value.valuesignificant = 1)
OR
(value.valuelift IS NULL AND value.valuesignificant IS NULL))
GROUP BY variable.variabletype
ORDER BY positive_occurrence DESC"
注意:如果存在变量可以value.valuelift IS NULL AND value.valuesignificant IS NULL
为真的合法情况,则会遇到问题。您可能需要添加value.variableid IS NULL
...在不查看数据的情况下很难说。