Mysql 内部联接不返回任何行
我试图从数据库中获取2015年4月没有任何事件的客户名称:Mysql 内部联接不返回任何行,mysql,inner-join,Mysql,Inner Join,我试图从数据库中获取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.D
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
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;
我得到零行,我应该有大约20个客户的名字在那里。查询有什么问题?您应该使用左联接,并且只需要在表中联接一次。但是,您不能选择活动日期,因为您获得的客户在此期间没有任何活动:
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, 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
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;
为什么在内部联接之后需要空Id?