简体   繁体   English

我在 SQL 数据库中有两个表,需要从一个表中获取基于分组的数据,其他正常

[英]I have two tables in a SQL database, need to get data based on group by from one table and other in normal

table 1: decks表 1:甲板

deckid甲板编号 name名称
1662490522316 1662490522316 test1测试1
1662975567468 1662975567468 test3测试3
1663153348829 1663153348829 test4/@/4测试4/@/4
1664289454461 1664289454461 Science Class EBP Physics 9th科学 Class EBP 物理 9th
1665037005819 1665037005819 d/@/dog d/@/狗

table 2: cards表 2:卡片

cardid心形 noteid记事本 deckid甲板编号
1635883239955 1635883239955 1635883239955 1635883239955 1664289454461 1664289454461
1635883239956 1635883239956 1635883239955 1635883239955 1664289454461 1664289454461
1635883343194 1635883343194 1635883343194 1635883343194 1664289454461 1664289454461
1635883343195 1635883343195 1635883343194 1635883343194 1664289454461 1664289454461
1635883382043 1635883382043 1635883382043 1635883382043 1664289454461 1664289454461
1635883382044 1635883382044 1635883382043 1635883382043 1664289454461 1664289454461
1635883439273 1635883439273 1635883439273 1635883439273 1664289454461 1664289454461
1635883439274 1635883439274 1635883439273 1635883439273 1664289454461 1664289454461
1635883509673 1635883509673 1635883509673 1635883509673 1664289454461 1664289454461
1635883509674 1635883509674 1635883509673 1635883509673 1664289454461 1664289454461
1662490587893 1662490587893 1662490587892 1662490587892 1662490522316 1662490522316
1662491389237 1662491389237 1662491389237 1662491389237 1662490522316 1662490522316
1662491433306 1662491433306 1662491433306 1662491433306 1662490522316 1662490522316
1662886491604 1662886491604 1662886491600 1662886491600 1662490522316 1662490522316
1662886955205 1662886955205 1662886955203 1662886955203 1662490522316 1662490522316
1662965836930 1662965836930 1662965836929 1662965836929 1662490522316 1662490522316
1663129181833 1663129181833 1663129181832 1663129181832 1662975567468 1662975567468
1663675409308 1663675409308 1663675409308 1663675409308 1663153348829 1663153348829
1663675409309 1663675409309 1663675409308 1663675409308 1663153348829 1663153348829
1663728830758 1663728830758 1663728830757 1663728830757 1662975567468 1662975567468
1664353381308 1664353381308 1664353381307 1664353381307 1662490522316 1662490522316
1664358364077 1664358364077 1664358364074 1664358364074 1662490522316 1662490522316
1664358364078 1664358364078 1664358364075 1664358364075 1662490522316 1662490522316
1665037065057 1665037065057 1665037065047 1665037065047 1665037005819 1665037005819

i want a query to return deckid, name and count of cards.我想要一个查询来返回卡片的牌号、名称和数量。 for each decks each deck will have n number of cards which is shown in table 2: cards对于每副牌,每副牌都有 n 张牌,如表 2 所示:

I did that with two queries 1)我用两个查询做到了 1)

SELECT decks.deckid, decks.name FROM decks

which returns返回

deckid甲板编号 name名称
1662490522316 1662490522316 test1测试1
1662975567468 1662975567468 test3测试3
1663153348829 1663153348829 test4/@/4测试4/@/4
1664289454461 1664289454461 Science Class EBP Physics 9th科学 Class EBP 物理 9th
1665037005819 1665037005819 d/@/dog d/@/狗

2) 2)

SELECT cards.deckid, count(cards.deckid) 
FROM cards 
GROUP BY cards.deckid

which returns number(or)count of cards in one deck id它返回一副牌 id 中卡片的数量(或)数量

deckid甲板编号 count数数
1662490522316 1662490522316 9 9
1662975567468 1662975567468 2 2个
1663153348829 1663153348829 2 2个
1664289454461 1664289454461 10 10
1665037005819 1665037005819 1 1个

i am expecting a query which returns this two result in to one like.我期待一个将这两个结果返回到一个类似结果的查询。

deckid甲板编号 name名称 count(crad.cardid)计数(crad.cardid)
1662490522316 1662490522316 test1测试1 9 9
1662975567468 1662975567468 test3测试3 2 2个
1663153348829 1663153348829 test4/@/4测试4/@/4 2 2个
1664289454461 1664289454461 Science Class EBP Physics 9th科学 Class EBP 物理 9th 10 10
1665037005819 1665037005819 d/@/dog d/@/狗 1 1个

You can link tables with JOIN if one to one, LEFT JOIN if one to many.如果一对一,您可以使用JOIN链接表,如果一对多,则可以使用LEFT JOIN链接表。

select d.deckid, d.name, COUNT(c.cardid) 
from
deck d 
left join cards c on c.deckid = d.deckid
group by
d.deckid, d.name
SELECT decks.deckid, decks.name, count(cards.cardid)
from decks
JOIN cards
ON decks.deckid = cards.deckid
GROUP BY cards.deckid

This one worked.这个奏效了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据其他两个表中的数据更新一个表 - Update one table based on data from two other tables Rails SQL连接两个表,一个表有两列,另一个表的ID,我需要获取这些ID的名称 - Rails SQL Join two tables, One table has two columns with ids of other table, I need to get names of these ids 双重联接可从基于其他两个表的表中获取数据 - Double Join to get data from a table based on other two tables 我可以从基于多个其他表的表中获取数据吗? - Can I get data from a table based on multiple other tables? 我有两个复选框,我需要根据我的sql值显示或隐藏一个或另一个 - I have two checkboxes, i need to show or hide one or the other based off my sql value 我在sql中有3个表如何从最后一个表中获取数据 - i have 3 tables in sql how to get data from last table SQL How to:从两个在其他命名列中具有2个相同数据的表中,需要对其中一个进行计数 - SQL How to: from two tables that has 2 same data in other named columns, one of them need to be counted SQL 语句检查数据是否存在于一个表中,而不存在于其他两个表中 - SQL statement to check if data exists in one table and not in two other tables 根据另外两个表从一个表中选择值(关系) - Select values from one table based on two other tables (relational) 我有两个通过某些键关联的表。 我需要编写查询以根据特定条件从两个表中获取数据 - I have two tables that are associated through some key. i need to write a query to get data from both the tables based on certain condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM