将NULL值替换为Where条件值

时间:2014-02-10 08:51:16

标签: mysql

我知道如何将NULL值替换为Where条件' John Miller'值

select 
  t2.creatorfullname, 
  count(distinct(t1.ticketmaskid)) as total
from 
  swtickets t1 join swticketauditlogs t2 
    on t1.ticketid = t2.ticketid 
where 
  from_unixtime(t2.dateline) > DATE_SUB(now(), INTERVAL 1 DAY) 
  and FIND_IN_SET(t2.creatorfullname,'John Miller') > 0 
  and t2.actionmsg like '%Ticket status changed from: % to: Closed%'  
  and totalreplies > 0;

表格

+-----------------+-------+
| creatorfullname | total |
+-----------------+-------+
| NULL            |     0 |
+-----------------+-------+
1 row in set (1.47 sec)

我想获得以下输出:

+-----------------+-------+
| creatorfullname | total |
+-----------------+-------+
| John Miller     |     0 |
+-----------------+-------+
1 row in set (1.47 sec)

如果where条件有多个名称怎么办?问题更新

select 
  t2.creatorfullname, 
  count(distinct(t1.ticketmaskid)) as total 
from 
  swtickets t1 join swticketauditlogs t2 
    on t1.ticketid = t2.ticketid 
where 
  from_unixtime(t2.dateline) > DATE_SUB(now(), INTERVAL 1 DAY) 
  and FIND_IN_SET(t2.creatorfullname,'John Miller, Alicia Lee, Steve Caleb') > 0 
  and t2.actionmsg like '%Ticket status changed from: % to: Closed%' 
  and totalreplies > 0;
+-----------------+-------+
| creatorfullname | total |
+-----------------+-------+
| NULL            |     0 |
| Alicia Lee      |     5 |
| Steve Caleb     |    43 |
+-----------------+-------+
1 row in set (1.47 sec)

我希望它是:

+-----------------+-------+
| creatorfullname | total |
+-----------------+-------+
| John Miller     |     0 |
| Alicia Lee      |     5 |
| Steve Caleb     |    43 |
+-----------------+-------+
1 row in set (1.47 sec)

4 个答案:

答案 0 :(得分:0)

试试这个......

SELECT IFNULL(t2.creatorfullname,'John Miller'), 
    COUNT(distinct(t1.ticketmaskid)) AS total
FROM swtic .....

答案 1 :(得分:0)

在您的对帐单中添加IFNULL项检查。 t2.creatorfullnameNULLFIND_IN_SET评估为完全

select t2.creatorfullname, count(distinct(t1.ticketmaskid)) as total from swtickets t1 join swticketauditlogs t2 on t1.ticketid = t2.ticketid where from_unixtime(t2.dateline) > DATE_SUB(now(), INTERVAL 1 DAY) and FIND_IN_SET(IFNULL(t2.creatorfullname,'x'),'John Miller') > 0 and t2.actionmsg like '%Ticket status changed from: % to: Closed%' and totalreplies > 0;

答案 2 :(得分:0)

您需要在包含名称列表的表和您的swticketauditlogs表之间建立外部联接:

SELECT
  t3.creatorfullname,
  COUNT(DISTINCT t1.ticketmaskid) total
FROM
  swtickets t1 JOIN swticketauditlogs t2 USING (ticketid) NATURAL RIGHT JOIN (
    SELECT 'John Miller' creatorfullname
  UNION ALL
    SELECT 'Alicia Lee'
  UNION ALL
    SELECT 'Steve Caleb'
  ) t3
WHERE
  t2.dateline > UNIX_TIMESTAMP(CURRENT_TIME - INTERVAL 1 DAY)
  AND t2.actionmsg LIKE '%Ticket status changed from: % to: Closed%' 
  AND totalreplies > 0
GROUP BY
  t3.creatorfullname

答案 3 :(得分:0)

使用GROUP BY

如果您想要某些用户进行过滤,请将该字段保留在WHERE子句中: (已编辑 - LEFT JOIN

select t2.creatorfullname
     , count(distinct(t1.ticketmaskid)) as total
from swticketauditlogs t2 
left join swtickets t1 on t1.ticketid = t2.ticketid 
where from_unixtime(t2.dateline) > DATE_SUB(now(), INTERVAL 1 DAY) 
  and FIND_IN_SET(t2.creatorfullname,'John Miller, Alicia Lee, Steve Caleb') > 0
  and t2.actionmsg like '%Ticket status changed from: % to: Closed%'
  and totalreplies > 0
group by
    t2.creatorfullname;