INNER JOIN 不返回任何行
INNER JOIN not returning any rows
我正在尝试从数据库中获取在 2015 年 4 月没有任何事件的客户姓名:
SELECT customer.customerId, customer.Name, event.Date
FROM customer
INNER JOIN event ON event.customerId=customer.customerId
INNER JOIN
(
SELECT eventId AS evid, customerId
FROM event
WHERE year(event.Date)=2015 AND month(event.Date)=04
GROUP BY customerId
)
EV ON event.eventId = EV.evid
WHERE event.customerId IS NULL
我得到零行,我应该有大约。那里有 20 个客户名称。查询有什么问题?
你应该使用左连接,你只需要在table中连接一次。但是,您不能 select 活动日期,因为您在 没有 间隔内的任何活动的情况下吸引客户:
select
customer.customerId, customer.Name
from
customer
left join event on event.customerId = customer.customerId
and year(event.Date) = 2015 and month(event.Date) = 4
where
event.customerId is null
这应该会为您提供在 2015 年 4 月没有任何活动的所有客户的客户 ID 和名称:
SELECT customer.customerId, customer.Name
FROM customer
WHERE customerId NOT IN
(
SELECT customerId
FROM event
WHERE year(event.Date)=2015 AND month(event.Date)=04
) EV;
我正在尝试从数据库中获取在 2015 年 4 月没有任何事件的客户姓名:
SELECT customer.customerId, customer.Name, event.Date
FROM customer
INNER JOIN event ON event.customerId=customer.customerId
INNER JOIN
(
SELECT eventId AS evid, customerId
FROM event
WHERE year(event.Date)=2015 AND month(event.Date)=04
GROUP BY customerId
)
EV ON event.eventId = EV.evid
WHERE event.customerId IS NULL
我得到零行,我应该有大约。那里有 20 个客户名称。查询有什么问题?
你应该使用左连接,你只需要在table中连接一次。但是,您不能 select 活动日期,因为您在 没有 间隔内的任何活动的情况下吸引客户:
select
customer.customerId, customer.Name
from
customer
left join event on event.customerId = customer.customerId
and year(event.Date) = 2015 and month(event.Date) = 4
where
event.customerId is null
这应该会为您提供在 2015 年 4 月没有任何活动的所有客户的客户 ID 和名称:
SELECT customer.customerId, customer.Name
FROM customer
WHERE customerId NOT IN
(
SELECT customerId
FROM event
WHERE year(event.Date)=2015 AND month(event.Date)=04
) EV;