Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 内部联接不返回任何行_Mysql_Inner Join - Fatal编程技术网

Mysql 内部联接不返回任何行

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

我试图从数据库中获取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
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?