简体   繁体   English

INNER JOIN中的子查询(MySQL)

[英]SubQuery in INNER JOIN (MySQL)

I seem to have a problem with a subquery in an inner join which doesn't do, what I'd. 我似乎在内连接中有一个子查询有问题,但是我没有。

  • Server: Localhost via UNIX socket 服务器:通过UNIX套接字的Localhost
  • Software: MySQL 软件:MySQL
  • Software version: 5.5.32-nmm2-log - (Ubuntu) 软件版本:5.5.32-nmm2-log - (Ubuntu)
  • Database client version: libmysql - 5.5.32 数据库客户端版本:libmysql - 5.5.32

There's an m:n table construct with 3 tables of which only 2 are relevant to the problem. 有一个m:n表构造,有3个表,其中只有2个与问题相关。

  • The first one is a table that contains actors, identifier: caid. 第一个是包含actor的表,标识符:caid。
  • The second one is the cross-table connects actors to movies (actor id: caid, movie id: id) 第二个是跨表连接演员到电影(演员ID:caid,电影ID:id)

When I create a full inner join like this: 当我像这样创建一个完整的内部联接:

SELECT count( * ) AS Count, lastname, firstname
FROM DVDPROFILER_dvd_common_actor
INNER JOIN DVDPROFILER_dvd_actor ON DVDPROFILER_dvd_common_actor.caid = DVDPROFILER_dvd_actor.caid
WHERE DVDPROFILER_dvd_actor.caid > 0
GROUP BY DVDPROFILER_dvd_actor.caid
ORDER BY Count DESC 

I get exactly what i'd expect: The top actors counted by the times he's credited in any movie, even if that's multiple times for multiple roles. 我得到的正是我所期待的:顶级演员按照他在任何电影中记入的时间计算,即使多次担任多个角色也是如此。

My goal is to extract the information of how many different movies an actor is profiled in and I thought - naive as I am - it should be as simple as this: 我的目标是提取有关演员被分析的电影的数量的信息,我认为 - 我很天真 - 它应该像这样简单:

SELECT count( * ) AS Count, lastname, firstname
FROM DVDPROFILER_dvd_common_actor
INNER JOIN
  (SELECT caid
  FROM DVDPROFILER_dvd_actor
  GROUP BY id) AS DVDPROFILER_dvd_actor 
ON DVDPROFILER_dvd_common_actor.caid = DVDPROFILER_dvd_actor.caid
WHERE DVDPROFILER_dvd_actor.caid > 0
GROUP BY DVDPROFILER_dvd_actor.caid
ORDER BY Count DESC 

But the results where totally incorrect 但结果完全不正确

So I checked the subquery with a selected actor 所以我用一个选定的actor检查了子查询

SELECT caid, id
FROM DVDPROFILER_dvd_actor
WHERE caid = 30801
GROUP BY id

And got exactly what I expected 并得到我所期望的

So I toyed a bit and when I introduced a LIMIT clause into the subquery I suddenly got different yet never correct replies. 所以我玩了一下,当我在子查询中引入LIMIT子句时,我突然得到了不同但从未正确的回复。

SELECT count( * ) AS Count, lastname, firstname
FROM DVDPROFILER_dvd_common_actor
INNER JOIN 
  (SELECT caid
  FROM DVDPROFILER_dvd_actor
  GROUP BY id
  LIMIT 0 , 50000) AS DVDPROFILER_dvd_actor 
ON DVDPROFILER_dvd_common_actor.caid = DVDPROFILER_dvd_actor.caid
WHERE DVDPROFILER_dvd_actor.caid > 0
GROUP BY DVDPROFILER_dvd_actor.caid
ORDER BY Count DESC 

With different LIMITs I got different results but at some point when I go over a certain limit, the result is exactly as it is without the limit - but equally wrong. 对于不同的LIMIT,我得到了不同的结果,但在某些时候,当我超过一定限度时,结果完全没有限制 - 但同样错误。

What am I overlooking here? 我在这里俯瞰什么? :-( :-(

could you try this? 你能试试吗? I guess COUNT(DISTINCT DVDPROFILER_dvd_actor.id) is helpful for you. 我想COUNT(DISTINCT DVDPROFILER_dvd_actor.id)对你有帮助。

SELECT lastname, firstname, COUNT(DISTINCT DVDPROFILER_dvd_actor.id) AS Count
FROM DVDPROFILER_dvd_common_actor
INNER JOIN DVDPROFILER_dvd_actor ON DVDPROFILER_dvd_common_actor.caid = DVDPROFILER_dvd_actor.caid
WHERE DVDPROFILER_dvd_actor.caid > 0
GROUP BY lastname, firstname
ORDER BY Count DESC 

If not, we are very happy when you post your data and schema on http://www.sqlfiddle.com/ . 如果没有,我们非常高兴您在http://www.sqlfiddle.com/上发布数据和架构。 that makes us easier to test 这使我们更容易测试

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM