结果来自 MYSQL 数据库,不是一个,而是几个表的结果

发布于 2024-11-26 14:47:43 字数 1213 浏览 4 评论 0原文

因此,我在这里需要查看 MYSQL 数据库的一些结果,这些结果不是基于一个表,而是基于几个表的结果。

表描述:

Metaexplanation:

Tablename

Row1,Row2,Row3....Row(N)

Tables:

---------------
|table toy  |
------------------------------ 
|toy_id | name | description  |
------------------------------


--------------------
|table owned_toy  |
----------------------------
| owner  | toy_id          |
----------------------------


--------------------
|table toy_expansions |
-------------------------------
| expansion | toy_id          |
-------------------------------

我创建了一个查询来识别没有分配所有者的玩具,

SELECT DISTINCT * from toy WHERE NOT EXISTS (SELECT * FROM ownedtoy WHERE owned_toy.toy_id = toy.id);

该查询有效,并且我能够识别没有所有者的东西。

现在,为了让事情变得更有趣,我试图找到一种方法来发现我在上面的查询中识别的每个玩具存在多少个扩展。

我可以使用以下方法计算玩具的扩展总数:

Select COUNT(expansion) from toy_expansions;

但是,我需要的是查看每个尚未拥有所有者的不同玩具的扩展总数,并且需要对其进行排序,以便我看到结果第一个查询与“扩展数量”列相结合,该列列出了被识别为没有所有者的每个玩具的扩展数量

我希望能够通过将原始查询与如下语句相结合来添加结果:

Select COUNT(expansion) as "Number of expansions" from toy_expansions

也只是让它变得更困难一点我希望能够在一个查询中执行它,而无需创建额外的表等,即无需对数据库本身进行任何更改。

有什么想法吗?

So here I am and need to see some results from a MYSQL database based not on one but on results from a few tables.

TABLE DESCRIPTION:

Metaexplanation:

Tablename

Row1,Row2,Row3....Row(N)

Tables:

---------------
|table toy  |
------------------------------ 
|toy_id | name | description  |
------------------------------


--------------------
|table owned_toy  |
----------------------------
| owner  | toy_id          |
----------------------------


--------------------
|table toy_expansions |
-------------------------------
| expansion | toy_id          |
-------------------------------

I created a query to identify toys with no owner assigned

SELECT DISTINCT * from toy WHERE NOT EXISTS (SELECT * FROM ownedtoy WHERE owned_toy.toy_id = toy.id);

That worked and I was able to identify things that had no owner.

Now to make things more interesting I am trying to find out a way to discover how many expansions exist for each of the toys that I have identified in the query above.

I can count the total number of expansions to the toys using:

Select COUNT(expansion) from toy_expansions;

However what I need is to see the total number of expansions for each DISTINCT toy that does not yet have an owner, and it needs to be sorted so that I see the results from the first query combined with a column saying "number of Expansions" that lists the number of expansions for each toy identified to have no owner

I would like to be able to adding the results by combining the original query with a statement like this:

Select COUNT(expansion) as "Number of expansions" from toy_expansions

Also just to make it a little more difficult I would like to be able to execute it in one single query without creating extra tables etc. i.e. without making any changes to the db itself.

Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

依 靠 2024-12-03 14:47:43
SELECT t.name, COUNT(te.expansion) AS NumberOfExpansions
    FROM toy t
        LEFT JOIN toy_expansions te
            ON t.id = te.toy_id
        LEFT JOIN owned_toy ot
            ON t.id = ot.toy_id
    WHERE ot.toy_id IS NULL
    GROUP BY t.name
SELECT t.name, COUNT(te.expansion) AS NumberOfExpansions
    FROM toy t
        LEFT JOIN toy_expansions te
            ON t.id = te.toy_id
        LEFT JOIN owned_toy ot
            ON t.id = ot.toy_id
    WHERE ot.toy_id IS NULL
    GROUP BY t.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文