MySQL复杂查询问题

发布于 2024-10-25 00:03:47 字数 1160 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-11-01 00:03:47

我认为您需要将子查询放入投影中,如下所示:

SELECT domains.*, clustercategories.clustercategoryname, 
(SELECT COUNT(*) FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1') AS total
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

I think that you'll want to put your subquery into your projection, like this:

SELECT domains.*, clustercategories.clustercategoryname, 
(SELECT COUNT(*) FROM subpages WHERE subpages.domainid = domains.id AND subpages.status = '1') AS total
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
屌丝范 2024-11-01 00:03:47

在我看来,您的第一个查询可以像这样重写

SELECT  d.*
        , cc.clustercategoryname
FROM    domains d
        INNER JOIN clustercategories cc
          ON cc.id = d.cluster1id 
          OR cc.id = d.cluster2id
WHERE   cc.id IN (3, 10)
        AND d.status = '1'                                           
GROUP BY 
        d.name
ORDER BY 
        d.name

,因此添加子页面的数量可以这样完成

  SELECT  d.*
          , cc.clustercategoryname
          , sp.total
  FROM    domains d
          INNER JOIN clustercategories cc 
            ON cc.id = d.cluster1id 
               OR cc.id = d.cluster2id
          LEFT OUTER JOIN (
            SELECT  COUNT(*) AS total
                    , domainid
            FROM    subpages
            WHERE   subpages.status = '1'
            GROUP BY
                    domainid
          ) sp ON sp.domainid = d.domainid          
  WHERE   cc.id IN (3, 10)
          AND d.status = '1'                                           
  GROUP BY 
          d.name
  ORDER BY 
          d.name

It looks to me your first query can be rewritten like this

SELECT  d.*
        , cc.clustercategoryname
FROM    domains d
        INNER JOIN clustercategories cc
          ON cc.id = d.cluster1id 
          OR cc.id = d.cluster2id
WHERE   cc.id IN (3, 10)
        AND d.status = '1'                                           
GROUP BY 
        d.name
ORDER BY 
        d.name

thus adding the count of subpages can be done like this

  SELECT  d.*
          , cc.clustercategoryname
          , sp.total
  FROM    domains d
          INNER JOIN clustercategories cc 
            ON cc.id = d.cluster1id 
               OR cc.id = d.cluster2id
          LEFT OUTER JOIN (
            SELECT  COUNT(*) AS total
                    , domainid
            FROM    subpages
            WHERE   subpages.status = '1'
            GROUP BY
                    domainid
          ) sp ON sp.domainid = d.domainid          
  WHERE   cc.id IN (3, 10)
          AND d.status = '1'                                           
  GROUP BY 
          d.name
  ORDER BY 
          d.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文