如何使用连接使用mySQL计数?

时间:2011-04-28 07:30:37

标签: mysql sql

我有以下数据库架构,我想获取所有类别的名称,但没有与该类别相关的引号。我写的查询只给了我一行,请你告诉我资源有效的查询。

enter image description here

3 个答案:

答案 0 :(得分:2)

SELECT SC.Name, Count(*) AS Quotes
FROM status_categories AS SC
INNER JOIN status_quotes AS SQ ON SC._id = SQ._category_id
GROUP BY SC.Name

答案 1 :(得分:1)

SELECT status_categories.NAME, COUNT(status_quotes.category_id)
FROM status_categories JOIN status_quotes ON status_categories._id = status_quotes.category_id
GROUP BY status_categories._id;

答案 2 :(得分:1)

尝试以下方法:

SELECT     `c`.`name`, COUNT(*) AS `Number of quotes`
FROM       `status_categories` AS `c`
INNER JOIN `status_quotes` AS `q`
    ON     `q`.`category_id` = `c`.`_id`
GROUP BY   `c`.`_id`;

修改

随意遗漏`字符。但这是安全的做法,即使它看起来有点讨厌。