0

So I'm reading this answer about selecting all columns of a table where only one of them is distinct, and it's exactly what I need, but unfortunately it's a MySQL question and not a django question, and I can't find a way to translate that to django.

The answer is very simple:

SELECT col1, col2 FROM mytable GROUP BY col2

But I can't find a way to do the same using django. Anything similar that gets me multiple columns of a model with one of them distinct will suffice.

I'm using MySQL and Django 1.9.

Thank you.

Community
  • 1
  • 1
Ella Sharakanski
  • 2,683
  • 3
  • 27
  • 47
  • I'm not sure I understand the question. In fact, the SQL that you are posting doesn't make any sense because a GROUP BY clause requires the use of an aggregate function on all the columns in the SELECT clause that are not in the GROUP BY. – Pat Jones Oct 24 '16 at 21:28
  • Have you looked at the answer in the link? It says "[this] will give you arbitrary unique col2 rows, with their col1 data as well" – Ella Sharakanski Oct 24 '16 at 21:29
  • It's also worth pointing out that you can embed SQL within your Python using the "mysql" module. If you have a straightforward and correct (see my above comment) SQL query, it might be worth querying directly versus relying upon Django's built-in capabilities. – Pat Jones Oct 24 '16 at 21:31

0 Answers0