Count Row from another table

Andi Wyder

I have this table structure

sites
id | name  | etc
1  | test  | etc
2  | test2 | etc
3  | test3 | etc

comments
id | site_id | comment
1  |    2    |   test comment
2  |    3    |   test2 comment
3  |    2    |   test3 comment

I want make a Select to list all sites eg.

test2
this is a test description
this is a test link
comments NR (2)

test3
this is a test description
this is a test link
comments NR (1)

I have this code to display the sites. Can anybody tell me how i can count the rows of the comment table?

$subcat_id = $_GET["id"];


        $stmt = $handler->prepare("SELECT *
                                    FROM sites sites
                                    LEFT JOIN comments comments ON ( sites.id = comments.site_id )
                                    WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
                                    GROUP BY sites.id
                                    ORDER BY RAND()");
        $stmt->execute();
        $no=$stmt->rowCount(); 
        while($row = $stmt->fetch(PDO::FETCH_OBJ)){
            $sites[]=$row;
        }

        $smarty->assign('site',$sites);
        $smarty->assign('anzahl',$no);

        $smarty->display('subcat.tpl');
MortenSickel

Rewrite your sql slightly:

SELECT *
FROM sites sites
LEFT JOIN comments comments ON ( sites.id = comments.site_id )
WHERE sites.subcat_id = '$subcat_id' AND sites.status = 1
ORDER BY sites.id

Now you know you will have consecutive comments for each site and you can check in php if there is a new site for which you should print out a header.

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

Join a columns count from another table

From Dev

SQL query with count() from another table

From Dev

Adding a count from another table to existing query

From Dev

Count from another table in FROM subquery

From Dev

mysql - count from different table supplemented with another count from subresult

From Dev

selecting records from main table and count of each row in another table

From Dev

Multiple row count from single table

From Dev

Trying to use subquery to get row count from another table

From Dev

How use count for value from another table

From Dev

SQLite select and count from another table with like

From Dev

Copy a row from one table to another

From Dev

Copying row from 1 table to another in SQL

From Dev

MySQL - Join & Count rows from another table

From Dev

Row value from another table

From Dev

How to count data from another table

From Dev

sql - show count of field from another table

From Dev

Select from one table and count from another

From Dev

Select * as well as count/sum from another table

From Dev

Update a specific row with data from another table

From Dev

How to count rows from another table to affect another table

From Dev

Jquery - copy a row from one table to another

From Dev

Automatic Count Row and update results to another table

From Dev

obtain row count of subset from data table

From Dev

SQL COUNT Rows from another table

From Dev

SQL Hide/Show rows based on row count from another table

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

Related Related

HotTag

Archive