如何比较两个表的计数
让Troble试图比较两个表(使用SQLite)的计数。
假设我有:
primary键)
numId | object_category |
---|---|
1 | 永久 |
2 | 借用 |
3 | 借用 |
4 | 借用 |
5 | 借用 |
okipts(numId is (numobj是 emern键/主键参考对象表)
numobj | collection_name |
---|---|
2 | collection_alpha |
3 | collection_betha |
4 | collection_betha collection_betha |
5 | collection_betha |
查询我需要执行的是:
“借入的对象数量比 数量的永久性objet?”
我目前的尝试是:
SELECT Collection_name FROM Borrowed
WHERE (SELECT COUNT(Collection_name) FROM Borrowed)>
(SELECT COUNT(*) FROM Objects WHERE Object_category =
'permanent')
它正在返回列中的值,而不是按照假定的方式工作(HOL应该返回拥有3个对象的'Collection_Betha',大于现有的永久对象的数量,即1)。
Having troble trying to compare count from two tables (using SQLite).
Supposing I have tables like:
Objects (NumId is Primary key)
NumId | Object_category |
---|---|
1 | permanent |
2 | borrowed |
3 | borrowed |
4 | borrowed |
5 | borrowed |
Borrowed
(NumObj is Foreign Key/Primary Key referring Object table)
NumObj | Collection_name |
---|---|
2 | collection_alpha |
3 | collection_betha |
4 | collection_betha |
5 | collection_betha |
Query I need to perform is:
"What are the collections with higher number of borrowed objects than
quantity of permanent objets?"
My current attempt is:
SELECT Collection_name FROM Borrowed
WHERE (SELECT COUNT(Collection_name) FROM Borrowed)>
(SELECT COUNT(*) FROM Objects WHERE Object_category =
'permanent')
It is returning the values from the column, not working as supposed (hould have returned the 'collection_betha' for owning 3 objects, greater than number of existing permanent objects that is 1).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许加入会有所帮助
Maybe a join will help
我将在这里使用有条件的聚合:
I would use conditional aggregation here: