Wednesday, May 31, 2017

Re: count from multiple tables in a single query?

On Wed, May 31, 2017 at 3:01 PM, Alceu Rodrigues de Freitas Junior <> wrote:

Hi James,

I'm curious... why dropping down from the ORM and doing a single query wouldn't be portable?

I understand that it would happen if you use some kind of stored procedure in the DB, but I guess a single ANSI SQL would do it.


- Alceu

I might have painted that statement with too broad of a brush, but the underlying intention is correct. Sticking with the ORM ensures consistent behavior across all supported database backends. In the case of a simple COUNT(*) query, even with multiple tables involved, the SQL statement is likely portable across any of the included backends. 

My intention was to say that using the ORM will better guarantee that the call is portable, not necessarily saying that raw SQL cannot or would not be. Of course, many queries can be made that would work in any SQL-compliant RDBMS. 

At any rate, a quick Google showed that the OP is likely stuck with two queries anyway. The SO posts, etc. show sub-SELECT statements in use in order to get the result returned as a single data set. 


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment