[英]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 ![]() |
test1![]() |
1662975567468 ![]() |
test3![]() |
1663153348829 ![]() |
test4/@/4![]() |
1664289454461 ![]() |
Science Class EBP Physics 9th![]() |
1665037005819 ![]() |
d/@/dog ![]() |
table 2: cards表 2:卡片
cardid![]() |
noteid![]() |
deckid![]() |
---|---|---|
1635883239955 ![]() |
1635883239955 ![]() |
1664289454461 ![]() |
1635883239956 ![]() |
1635883239955 ![]() |
1664289454461 ![]() |
1635883343194 ![]() |
1635883343194 ![]() |
1664289454461 ![]() |
1635883343195 ![]() |
1635883343194 ![]() |
1664289454461 ![]() |
1635883382043 ![]() |
1635883382043 ![]() |
1664289454461 ![]() |
1635883382044 ![]() |
1635883382043 ![]() |
1664289454461 ![]() |
1635883439273 ![]() |
1635883439273 ![]() |
1664289454461 ![]() |
1635883439274 ![]() |
1635883439273 ![]() |
1664289454461 ![]() |
1635883509673 ![]() |
1635883509673 ![]() |
1664289454461 ![]() |
1635883509674 ![]() |
1635883509673 ![]() |
1664289454461 ![]() |
1662490587893 ![]() |
1662490587892 ![]() |
1662490522316 ![]() |
1662491389237 ![]() |
1662491389237 ![]() |
1662490522316 ![]() |
1662491433306 ![]() |
1662491433306 ![]() |
1662490522316 ![]() |
1662886491604 ![]() |
1662886491600 ![]() |
1662490522316 ![]() |
1662886955205 ![]() |
1662886955203 ![]() |
1662490522316 ![]() |
1662965836930 ![]() |
1662965836929 ![]() |
1662490522316 ![]() |
1663129181833 ![]() |
1663129181832 ![]() |
1662975567468 ![]() |
1663675409308 ![]() |
1663675409308 ![]() |
1663153348829 ![]() |
1663675409309 ![]() |
1663675409308 ![]() |
1663153348829 ![]() |
1663728830758 ![]() |
1663728830757 ![]() |
1662975567468 ![]() |
1664353381308 ![]() |
1664353381307 ![]() |
1662490522316 ![]() |
1664358364077 ![]() |
1664358364074 ![]() |
1662490522316 ![]() |
1664358364078 ![]() |
1664358364075 ![]() |
1662490522316 ![]() |
1665037065057 ![]() |
1665037065047 ![]() |
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 ![]() |
test1![]() |
1662975567468 ![]() |
test3![]() |
1663153348829 ![]() |
test4/@/4![]() |
1664289454461 ![]() |
Science Class EBP Physics 9th![]() |
1665037005819 ![]() |
d/@/dog ![]() |
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 ![]() |
9 ![]() |
1662975567468 ![]() |
2 ![]() |
1663153348829 ![]() |
2 ![]() |
1664289454461 ![]() |
10 ![]() |
1665037005819 ![]() |
1 ![]() |
i am expecting a query which returns this two result in to one like.我期待一个将这两个结果返回到一个类似结果的查询。
deckid![]() |
name![]() |
count(crad.cardid)![]() |
---|---|---|
1662490522316 ![]() |
test1![]() |
9 ![]() |
1662975567468 ![]() |
test3![]() |
2 ![]() |
1663153348829 ![]() |
test4/@/4![]() |
2 ![]() |
1664289454461 ![]() |
Science Class EBP Physics 9th![]() |
10 ![]() |
1665037005819 ![]() |
d/@/dog ![]() |
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.