到目前为止,我已经让它与子选择一起使用,但一些研究告诉我,使用子选择(特别是在大表上)是不好的,因为它们的性能较差。
现在这就是我得到的:
SELECT COUNT( riddims.riddim ) AS rc,
(
SELECT COUNT( tunes.tune )
FROM tunes
WHERE tunes.tune NOT
IN (
''
)
) AS tc
FROM riddims
WHERE riddims.riddim NOT
IN (
''
)
表格看起来像:
riddims:
riddim | genre | image
tunes:
riddim | artist | tune
我正在尝试“JOIN”,但无法真正找出有效的查询。我需要的是类似于 STACKOVERFLOW COUNT FROM MULTIPLE TABLES 的东西以比我上面的解决方案更高效的方式。
我的目标是执行显示以下输出的查询:
riddims | tunes | artist
100 | 400 | 2
- Riddims 不在 ('') 中
- WHERE 曲子不在('')
- WHERE 艺术家=“某位艺术家”
这就是我开始的方式,但显然走向了错误的方向:
SELECT COUNT(riddims.riddim) AS rc, COUNT(tunes.tune) AS tc FROM riddims LEFT JOIN tunes ON riddims.riddim = tunes.riddim
最佳答案
您是否尝试这样做:
select riddims, tunes, artists
from (select count(*) as riddims from riddims where . . . ) r cross join
(select count(*) as tunes from tunes where tunes not in . . .) t cross join
(select count(*) as artists from tunes where artist not in . . .) a
您的表似乎没有连接,至少对于这个查询来说是这样。可能的性能问题是,每行都会调用一次 select 中的子查询。通过将它们放入 FROM 子句中,可以消除这个可能的问题。
关于MySQL 使用 WHERE 语句跨表选择 COUNT()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380160/