<分区>
我正在处理一个 SQL,我需要在其中获取每个帖子的评论数。我在这里使用三个表来使用用户、帖子和评论。帖子和评论有 postid 作为外键。
当我尝试获取评论数时出现以下错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM posts, users where users.userid = posts.userid and posts.userid = 16 order ' at line 1
我的SQL:
SELECT
postid,
post,
type,
users.userid,
CONCAT( firstname, ' ', lastname ) as name,
COALESCE(
(SELECT
COUNT(*)
FROM comments
WHERE postid = posts.postid
),
0) AS cnt,
FROM
posts,
users
where
users.userid = posts.userid and
posts.userid = 16
order by
postid desc