Adding a count from another table to existing query

Toni Michel Caubet

This query is working fine:

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

But now I would like to add how many comments has a post, which is in the comentarios table.

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

The thing is that the mysql error is

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

Any idea what am I missing here?

Filipe Silva

Try something like this:

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

This will left join with the number of comments per post, and if the JOIN is not matched will show 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL query INSERT SELECT COUNT from one table to another row by row

From Dev

MySQL query with COUNT and join column from another table

From Dev

SQL query with count() from another table

From Dev

Count from another table in FROM subquery

From Dev

Selecting SUM+COUNT from one table and COUNT from another in Single query

From Dev

Adding rows from one table to another existing table where primary key is autogenerated

From Dev

Adding new data from JSON to existing table

From Dev

Modify existing query from an unrelated table

From Dev

Select query to count results based on another table

From Dev

How to get count from another table (zero when no data) using single query in mysql?

From Dev

Adding a column to existing table

From Dev

Mysql Query with LEFT JOIN and having count of OrderID from another table... Stuck

From Dev

Count Row from another table

From Dev

Count number of rows from another table in single query

From Dev

adding table to an existing query

From Dev

Count rows from another SQL table in the same query

From Dev

Query within another query to count amount of items from another table

From Dev

Adding rows from one table to another existing table where primary key is autogenerated

From Dev

Mysql query with count on another table

From Dev

SQL Adding count (from 2 tables) to existing select (of 3 tables)

From Dev

Adding a row to an existing table

From Dev

Adding count(*) to existing mysql query causes unwanted action in php

From Dev

Select from one table and count from another

From Dev

MySQL query - select from one, count from another table

From Dev

Adding a join to a lookkup table in existing query?

From Dev

Filter by count from another table

From Dev

MYSQL select from table and count from another

From Dev

Get count from another table

From Dev

MySQL CASE with COUNT query and adding another column

Related Related

  1. 1

    SQL query INSERT SELECT COUNT from one table to another row by row

  2. 2

    MySQL query with COUNT and join column from another table

  3. 3

    SQL query with count() from another table

  4. 4

    Count from another table in FROM subquery

  5. 5

    Selecting SUM+COUNT from one table and COUNT from another in Single query

  6. 6

    Adding rows from one table to another existing table where primary key is autogenerated

  7. 7

    Adding new data from JSON to existing table

  8. 8

    Modify existing query from an unrelated table

  9. 9

    Select query to count results based on another table

  10. 10

    How to get count from another table (zero when no data) using single query in mysql?

  11. 11

    Adding a column to existing table

  12. 12

    Mysql Query with LEFT JOIN and having count of OrderID from another table... Stuck

  13. 13

    Count Row from another table

  14. 14

    Count number of rows from another table in single query

  15. 15

    adding table to an existing query

  16. 16

    Count rows from another SQL table in the same query

  17. 17

    Query within another query to count amount of items from another table

  18. 18

    Adding rows from one table to another existing table where primary key is autogenerated

  19. 19

    Mysql query with count on another table

  20. 20

    SQL Adding count (from 2 tables) to existing select (of 3 tables)

  21. 21

    Adding a row to an existing table

  22. 22

    Adding count(*) to existing mysql query causes unwanted action in php

  23. 23

    Select from one table and count from another

  24. 24

    MySQL query - select from one, count from another table

  25. 25

    Adding a join to a lookkup table in existing query?

  26. 26

    Filter by count from another table

  27. 27

    MYSQL select from table and count from another

  28. 28

    Get count from another table

  29. 29

    MySQL CASE with COUNT query and adding another column

HotTag

Archive