MySQL查询计数输出
我试图从同一查询中的另一个SELECT中获取信息,但是我不太确定如何获取所需的字段MySQL查询计数输出,mysql,Mysql,我试图从同一查询中的另一个SELECT中获取信息,但是我不太确定如何获取所需的字段 SELECT t.`id`, t.`title`, t.`author`, t.`content`, ctitle, cid, comments FROM `tutorials` AS `t`, ( SELECT tc.`id` as `cid`, tc.`title` as `ctitle` FROM `tutorial_categories` AS `tc` WHERE
SELECT t.`id`, t.`title`, t.`author`, t.`content`, ctitle, cid, comments
FROM `tutorials` AS `t`,
(
SELECT tc.`id` as `cid`, tc.`title` as `ctitle`
FROM `tutorial_categories` AS `tc`
WHERE `title` LIKE '%category title'
) AS `c`,
(
SELECT COUNT(com.`id`) as `comments`
FROM `tutorial_comments` AS `com`
WHERE `tutorial_id` = c.cid
) as `comments`
WHERE t.`category` = c.`cid` AND t.`status` = '1'
ORDER BY `id` ASC
我正在尝试从tutorial\u类别
中获取id,并在tutorial\u注释
中使用它。作为最终输出,我所要做的就是计算每个教程列出的注释数量
干杯
雅各布你能试试这个吗:
SELECT t.`id`, t.`title`, t.`author`, t.`content`, c.title, c.cid, ct.comments
FROM `tutorials` AS `t`
LEFT OUTER JOIN (
SELECT tc.`id` as `cid`, tc.`title` as `ctitle`
FROM `tutorial_categories` AS `tc`
WHERE `title` LIKE '%category title'
) AS `c` ON t.`category` = c.`cid`
LEFT OUTER JOIN (
SELECT COUNT(com.`id`) as `comments`
FROM `tutorial_comments` AS `com`
group by com.`id`
) as `ct` on ct.`tutorial_id` = c.cid
WHERE t.`status` = '1'
ORDER BY `id` ASC
你能试试这个吗
SELECT t.`id`, t.`title`, t.`author`, t.`content`, c.title, c.cid, ct.comments
FROM `tutorials` AS `t`
LEFT OUTER JOIN (
SELECT tc.`id` as `cid`, tc.`title` as `ctitle`
FROM `tutorial_categories` AS `tc`
WHERE `title` LIKE '%category title'
) AS `c` ON t.`category` = c.`cid`
LEFT OUTER JOIN (
SELECT COUNT(com.`id`) as `comments`
FROM `tutorial_comments` AS `com`
group by com.`id`
) as `ct` on ct.`tutorial_id` = c.cid
WHERE t.`status` = '1'
ORDER BY `id` ASC
雅各布
您需要像这样添加一个GROUPBY子句
select t.id, t.tilte, t.author, t.content, count(com.id) as comments
from tutorials as t
join tutotials_categories as cat
on t.category = cat.id
join tutorials_comments as com
on com.tutorial_id = t.id
where cat.title like'%category title'
and t.status = 1
group by com.id
order by t.id asc
我使用了ansi连接形式
您需要像这样添加一个GROUPBY子句
select t.id, t.tilte, t.author, t.content, count(com.id) as comments
from tutorials as t
join tutotials_categories as cat
on t.category = cat.id
join tutorials_comments as com
on com.tutorial_id = t.id
where cat.title like'%category title'
and t.status = 1
group by com.id
order by t.id asc
我使用了ansi连接表单这应该可以清理您的查询:
SELECT t.id, t.title, t.author, t.content, c.ctitle, c.cid, com.comments
FROM tutorials AS t
LEFT JOIN (
SELECT tutorial_id, COUNT(com.id) as comments
FROM tutorial_comments AS com
GROUP BY 1
) AS com ON com.tutorial_id = t.category
LEFT JOIN (
SELECT tc.id as cid, tc.title as ctitle
FROM tutorial_categories AS tc
WHERE title LIKE '%category title'
) AS c ON t.category = c.cid
WHERE t.status = '1'
ORDER BY t.id
左连接可防止找不到匹配项的教程消失。我用连接条件进行了显式连接,这更容易理解,也是正确的方法。
您的主要问题是,您的计数注释的连接条件在括号内,而不是在括号外,这样做是行不通的。这应该会清理您的查询:
SELECT t.id, t.title, t.author, t.content, c.ctitle, c.cid, com.comments
FROM tutorials AS t
LEFT JOIN (
SELECT tutorial_id, COUNT(com.id) as comments
FROM tutorial_comments AS com
GROUP BY 1
) AS com ON com.tutorial_id = t.category
LEFT JOIN (
SELECT tc.id as cid, tc.title as ctitle
FROM tutorial_categories AS tc
WHERE title LIKE '%category title'
) AS c ON t.category = c.cid
WHERE t.status = '1'
ORDER BY t.id
左连接可防止找不到匹配项的教程消失。我用连接条件进行了显式连接,这更容易理解,也是正确的方法。
您的主要问题是,您在括号内而不是括号外有计数的注释的连接条件,这无法实现。现在它只显示有注释的结果,而不是每个类别的所有结果。很抱歉,我没有理解您的意思。您可以尝试“左连接”来显示那些没有注释的教程,还可以按类别排序以对结果集进行排序。顺便说一句,我想你可以试试@erwin的脚本。现在它只显示有评论的结果,而不是每个类别的所有结果。很抱歉,我没有理解你的意思。您可以尝试“左连接”来显示那些没有注释的教程,还可以按类别排序以对结果集进行排序。顺便说一句,我想你可以试试@erwin的脚本。