如果没有找到行,则在算术运算中使用一些默认值

时间:2018-05-27 10:29:09

标签: mysql database

我有两张桌子:

booking(advertiser_id,  name, bookings, on_date)
click(advertiser_id, name, clicks, on_date)

我需要为每个bookings/clicks advertiser_id-wise date-wise 找到name。我正在做以下事情来实现这个目标:

select b.name, b.advertiser_id, max(b.bookings)/max(c.clicks), b.on_date from click c inner join booking b on 
    c.advertiser_id = b.advertiser_id and 
    c.name = b.name and
    c.on_date = b.on_date
group by
    b.name,
    b.advertiser_id,
    b.on_date

如果特定点击没有预订(预订表中没有条目),我需要返回0。我怎样才能做到这一点?

示例:

click表:

name clicks on_date    advertiser_id
uk   123    2018-05-01 12
us   123    2018-05-02 12
us   123    2018-05-01 12

booking表:

advertiser_id name bookings on_date
12            uk   1200     2018-05-07 
12            us   123      2018-05-07
12            uk   123      2018-05-01
12            us   123      2018-05-01

结果:

name advertiser_id max(b.bookings)/max(c.clicks) on_date
uk   12            1.0000                        2018-05-01
us   12            1.0000                        2018-05-01

预期:

name advertiser_id max(b.bookings)/max(c.clicks) on_date
uk   12            1.0000                        2018-05-01
us   12            1.0000                        2018-05-01
us   12            0                             2018-05-02

注意

我使用max来使用group by中不存在的列。

2 个答案:

答案 0 :(得分:1)

要考虑的事情......

DROP TABLE IF EXISTS click;

CREATE TABLE click
(name CHAR(2) NOT NULL
,clicks INT NOT NULL
,on_date DATE NOT NULL
,advertiser_id INT NOT NULL
,PRIMARY KEY(name,on_date,advertiser_id)
);

INSERT INTO click VALUES
('uk',123,'2018-05-01',12),
('us',123,'2018-05-02',12),
('us',123,'2018-05-01',12);


DROP TABLE IF EXISTS booking;

CREATE TABLE booking 
(advertiser_id INT NOT NULL
,name CHAR(2) NOT NULL
,bookings INT NOT NULL
,on_date DATE NOT NULL
,PRIMARY KEY(advertiser_id,name,on_date)
);

INSERT INTO booking VALUES
(12,'uk',1200,'2018-05-07'),
(12,'us', 123,'2018-05-07'),
(12,'uk', 123,'2018-05-01'),
(12,'us', 123,'2018-05-01');


select c.name
     , c.advertiser_id
     , COALESCE(b.bookings,0)
     , c.clicks
     , c.on_date 
  from click c 
  left
  join booking b 
    on c.advertiser_id = b.advertiser_id 
   and c.name = b.name 
   and c.on_date = b.on_date;

+------+---------------+------------------------+--------+------------+
| name | advertiser_id | COALESCE(b.bookings,0) | clicks | on_date    |
+------+---------------+------------------------+--------+------------+
| uk   |            12 |                    123 |    123 | 2018-05-01 |
| us   |            12 |                    123 |    123 | 2018-05-01 |
| us   |            12 |                      0 |    123 | 2018-05-02 |
+------+---------------+------------------------+--------+------------+   

答案 1 :(得分:0)

您可以使用LEFT JOIN执行此操作,因此您的查询应该如下,

select C.name,c.advertiser_id, IFNULL(max(b.bookings)/max(c.clicks), 0),c.on_date
FROM click c
LEFT join booking b on
   c.advertiser_id = b.advertiser_id and 
   c.name = b.name and
   c.on_date = b.on_date
group by
   b.name,
   b.advertiser_id,
   b.on_date

它完美无缺。

打开LINK查看输出。