MySQL复杂查询问题
我的 MySQL 查询有问题: 我有两张桌子: - 集群类别 - 域
现在我有一个 SQL 查询,其中列出了具有类别名称的特定类别的所有域 - 这是我的查询:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
现在的问题是,我还有第三个表“子页面”,我想在其中计算特定类别的所有条目状态 = '1' 的域,我不知道如何修改我的查询以使其工作 - 我已经尝试过此查询,但我没有得到任何结果:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1'))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1')))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
有人有任何想法吗?
I have a problem with a MySQL Query:
I have two tables:
- clustercategories
- domains
Now I have a SQL Query which lists all Domains of a specific category with category name - this is my Query:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10)))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
The Problem is now, that I also have a third table "subpages" where I want to count all entries of a specific domain with status = '1' and I don't know how to modify my query to work - I have tried this query, but I do not get any results:
SELECT domains.*, clustercategories.clustercategoryname
FROM (domains, clustercategories)
WHERE ((clustercategories.id = 3 AND (domains.cluster1id = 3 OR domains.cluster2id = 3) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1'))
OR (clustercategories.id = 10 AND (domains.cluster1id = 10 OR domains.cluster2id = 10) AND (SELECT COUNT(*) AS total FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1')))
AND domains.status = '1'
GROUP BY domains.name
ORDER BY domains.name
Has anyone any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要将子查询放入投影中,如下所示:
I think that you'll want to put your subquery into your projection, like this:
在我看来,您的第一个查询可以像这样重写
,因此添加子页面的数量可以这样完成
It looks to me your first query can be rewritten like this
thus adding the count of subpages can be done like this