结果来自 MYSQL 数据库,不是一个,而是几个表的结果
因此,我在这里需要查看 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)