2013-11-14 53 views
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

任何想法我在這裏錯過了什麼?

回答

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.

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

+0

嗨,那裏,謝謝你的時間。像這個查詢現在工作,問題是我沒有想到這一點,只是回覆評論的帖子,我想評論不要過濾查詢,只是添加每個帖子的評論數,是那麼棘手? –

+0

我更新了sql(你可能需要也可能不需要「group by」 - 不知道關於mysql) – Gerrat

0

你面前有一個逗號從第一查詢,但不得早於在第二個查詢

相關問題