amani rose
amani rose

Reputation: 121

Two queries in one statment

I want in put two queries in one statement how can i do that in this state?

1
stmt = `SELECT Comments.*, Users.username,Users.avatar from Users 
        INNER JOIN Comments ON Comments.users_id =Users.users_id 
        WHERE Comments.post_id= 1`
2
`SELECT COUNT(*) comment FROM Comments WHERE Comments.post_id= 1`;

Upvotes: 0

Views: 83

Answers (3)

Abhishek Attri
Abhishek Attri

Reputation: 114

You can use GROUP BY on comments table in a way:

'SELECT COUNT(Comments.<id>), Comments.*, Users.username,Users.avatar 
 from Users INNER JOIN Comments ON Comments.users_id =Users.users_id 
 WHERE Comments.post_id = 1 GROUP BY Comments.<id>';

*This syntax of GROUP BY clause follows PostgreSQL. You might need to tweak according to the syntax followed by sqlite.

Upvotes: 0

forpas
forpas

Reputation: 164069

Cross join the 1st query to the 2nd:

SELECT c.*, u.username, u.avatar, t.counter 
FROM Users u INNER JOIN Comments c
ON c.users_id = u.users_id 
CROSS JOIN (SELECT COUNT(*) counter FROM Comments WHERE post_id = 1) t
WHERE c.post_id = 1

Upvotes: 1

Jonas Heinze
Jonas Heinze

Reputation: 53

I don't know much about SQLite, but in SQL Server you can use ";" to use multiple queries. Maybe it does work for SQLite.

Upvotes: 0

Related Questions