简体   繁体   English

当其他列为空时,如何在mysql列中显示结果?

[英]How do i show results in mysql column when other column is empty?

I have the following query: 我有以下查询:

SELECT 
    (IFNULL(daily_clicks, 0)), 
    (IFNULL(daily_score, 0)), 
    (SELECT SUM(`daily_score`) FROM `user_score` `n`) AS total 
FROM
`user_score` `t` WHERE `userid`="123123" AND t.created='2015-06-27' LIMIT 1;

If i change the date to where data exist i see the total user_score, but when i select where there are no results for that specific date, i get nothing back (even in the sum). 如果我将日期更改为存在数据的日期,则会看到总的user_score,但是当我选择该特定日期没有结果的位置时,我什么也收不到(即使是总和)。

How do i get the sum column to appear even if other does not? 即使其他没有显示,我如何显示总和列? Thx 谢谢

If the date doesn't exist, then the query returns no rows. 如果日期不存在,则查询不返回任何行。

One method is to use aggregation: 一种方法是使用聚合:

SELECT coalesce(sum(daily_clicks), 0), 
       coalesce(sum(daily_score), 0)
FROM `user_score` `t`
WHERE `userid` = 123123 AND t.created = '2015-06-27';

This will guarantee that one row returns, even when there are no matches. 即使没有匹配项,这也将确保返回一行。 If you want each daily_click , you can use group_concat() : 如果需要每个daily_click ,则可以使用group_concat()

SELECT group_concat(daily_clicks),
       coalesce(sum(daily_score), 0)
FROM `user_score` `t`
WHERE `userid` = 123123 AND t.created = '2015-06-27';

This still returns one row, but with multiple daily_clicks values concatenated in one column. 这仍然返回一行,但在一列中串联了多个daily_clicks值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM