1

此查询工作正常:

SELECT  posts.titulo as value,
                    posts.id as id,
                    posts.img_src as img,
                    posts.id_familia,
                    posts.tiempo,
                    posts.votos,
                    familias.clave,
                    familias.id as fm,
                    textos.clave,
                    textos.texto as familia,
            FROM posts,familias,textos
            WHERE posts.id_familia = familias.id AND familias.clave = textos.clave AND textos.lengua = ".detectarIdioma()." 
            and posts.id_usuario = $term 
            ORDER BY posts.id DESC

但是现在我想在评论表中添加有多少评论有一个帖子。

SELECT  posts.titulo as value,
                    posts.id as id,
                    posts.img_src as img,
                    posts.id_familia,
                    posts.tiempo,
                    posts.votos,
                    familias.clave,
                    familias.id as fm,
                    textos.clave,
                    textos.texto as familia,
                    count(comentarios.id)
            FROM posts,familias,textos
            JOIN comentarios ON comentarios.id_post = posts.id
            WHERE posts.id_familia = familias.id AND familias.clave = textos.clave AND textos.lengua = ".detectarIdioma()." 
            and posts.id_usuario = $term 
            ORDER BY posts.id DESC

问题是mysql错误是

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,familias,textos JOIN comentarios ON ' at line 12

知道我在这里想念什么吗?

4

3 回答 3

1

尝试这样的事情:

SELECT posts.titulo AS value,
  posts.id AS id,
  posts.img_src AS img,
  posts.id_familia,
  posts.tiempo,
  posts.votos,
  familias.clave,
  familias.id AS fm,
  textos.clave,
  textos.texto AS familia,
  COALESCE(COM_COUNT.NUM_COMMENTS,0) AS num_comments
FROM posts 
INNER JOIN familias ON posts.id_familia = familias.id
INNER JOIN textos familias.clave = textos.clave
LEFT JOIN 
    ( SELECT id_post, COUNT(*) AS NUM_COMMENTS
      FROM comentarios
      GROUP BY id_post
    ) COM_COUNT ON COM_COUNT.id_post = posts.id   
WHERE AND textos.lengua = ".detectarIdioma()."
  AND posts.id_usuario = $TERM
ORDER BY posts.id DESC

这将留下每个帖子的评论数,如果 JOIN 不匹配将显示 0。

于 2013-11-14T22:52:53.923 回答
0

尝试这个:

SELECT  posts.titulo as value,
        posts.id as id,
        posts.img_src as img,
        posts.id_familia,
        posts.tiempo,
        posts.votos,
        familias.clave,
        familias.id as fm,
        textos.clave,
        textos.texto as familia,
        count(comentarios.id)
FROM posts INNER JOIN familias ON posts.id_familia = familias.id 
INNER JOIN textos ON familias.clave = textos.clave 
LEFT OUTER JOIN comentarios ON comentarios.id_post = posts.id
WHERE textos.lengua = ".detectarIdioma()." 
AND posts.id_usuario = $term 
GROUP BY posts.titulo,
        posts.id,
        posts.img_src,
        posts.id_familia,
        posts.tiempo,
        posts.votos,
        familias.clave,
        familias.id,
        textos.clave,
        textos.texto
ORDER BY posts.id DESC

您只是混合了两种类型的JOIN语法。...并且您可能需要按除您正在计数的那一列之外的每一列进行分组。

编辑:为了不将您的结果限制为仅具有评论的结果,您需要LEFT OUTER JOIN在该表上执行一个。

于 2013-11-14T22:43:05.860 回答
0

您在第一个查询中的 FROM 之前有一个逗号,但在第二个查询中的 FROM 之前没有

于 2013-11-14T22:51:59.297 回答