S
S
Sergey Eremin2014-11-11 14:05:23
Django
Sergey Eremin, 2014-11-11 14:05:23

How to make a SQL query with INNER JOIN in Django via ORM?

Here's how to make such a simple, seemingly request, through the ORM?

SELECT  TAB2.option, Count(TAB1.id) AS CountObj
FROM TAB1 INNER JOIN TAB2 ON TAB1.Link = TAB2.id
GROUP BY TAB2.option
ORDER BY Count(TAB1.id) DESC;

I'm not a real welder yet, and so far everything that turns out slows down. Checked up - ORM builds a heap of requests instead of one GROUP BY. I can't figure out how to get it to build correctly. Which model, you can guess from the request ... However, this is not the point of how important it is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Kuzmichev, 2014-11-11
@Sergei_Erjemin

annotate will help you
Read more here: https://docs.djangoproject.com/en/dev/topics/db/ag... Seems
like a good example with annotate and sorting by count(): stackoverflow.com/questions/1396264/ how-to-sort-by...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question