CREATE TABLE logistics (
id int primary key,
campaign VARCHAR(255),
inbound_date VARCHAR(255),
outbound_date VARCHAR(255)
);
INSERT INTO logistics
(id, campaign, inbound_date, outbound_date)
VALUES
("1", "C001", "2019-01-01", "2019-02-08"),
("2", "C001", "2019-05-10", "2019-05-12"),
("3", "C001", "2019-06-12", "2019-06-15"),
("4", "C001", "2019-08-13", "2019-08-20"),
("5", "C001", "2019-11-14", "2019-11-22");
在上表中我有以下列inbound_date
and outbound_date
对于日期值。
在我的查询中我coalesce
他们到一个名为event_date
.
现在,我想使用别名coalesce
in the WHERE
我的查询子句,但出现错误Unknown column 'event_date' in 'where clause'
:
SELECT
campaign,
coalesce(inbound_date, outbound_date) as event_date
FROM logistics
WHERE
event_date BETWEEN "2019-06-01" AND "2019-10-01"
我知道我可以通过使用来解决这个问题inbound_date
and outbound_date
作为两个单独的条件WHERE
子句,但没有更聪明的方法使用别名coalesce
?