gpt4 book ai didi

MySQL 列出带有链接的类别

转载 作者:行者123 更新时间:2023-11-29 12:21:52 25 4
gpt4 key购买 nike

抱歉,我会说一点英语。没关系:

SELECT * FROM `category` WHERE `state` = TRUE ORDER BY `title` ASC

我想要:

SELECT *, cnt FROM `category` WHERE `state` = TRUE ORDER BY `title` ASC

cnt为count link(category-id)/category(id)。

CREATE TABLE category ( id );
CREATE TABLE link ( id, category-id ); -- link(category-id)=category(id)

每个类别有多少个链接?

谢谢。

最佳答案

加入link表并按要从category表中选择的所有列进行分组

SELECT c.id, c.title, count(l.id) as cnt 
FROM `category` c
left join link l on l.category_id = c.id
WHERE c.`state` = 1
GROUP BY c.id, c.title
ORDER BY c.`title` ASC

关于MySQL 列出带有链接的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28885766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com