DISTINCT ON 字段在 MySQL 上不可用。您使用哪个等效项?
DISTINCT ON field is not available on MySQL. Which equivalent do you use?
假设我有一个模型:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
我想通过搜索 first_name
的第一个字母来获得 Person
的列表。但是每一个肯定有不同的last_name
.
例如:
在以下列表中搜索 M...
:
Frank Edmond
Marc Thomas
Matthew Ronald
Matthew Smith
Matthew Thomas
Richard Thomas
会给你
Marc Thomas
Matthew Ronald
Matthew Smith
简而言之,如何在MySQL中翻译:
Person.objects.filter(first_name__startswith='M').distinct('last_name')
在MySQL
SELECT DISTINCT <table>.last_name FROM <table> WHERE <table>.first_name LIKE 'M%'
将 <table>
替换为您的 table 姓名。
假设我有一个模型:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
我想通过搜索 first_name
的第一个字母来获得 Person
的列表。但是每一个肯定有不同的last_name
.
例如:
在以下列表中搜索 M...
:
Frank Edmond
Marc Thomas
Matthew Ronald
Matthew Smith
Matthew Thomas
Richard Thomas
会给你
Marc Thomas
Matthew Ronald
Matthew Smith
简而言之,如何在MySQL中翻译:
Person.objects.filter(first_name__startswith='M').distinct('last_name')
在MySQL
SELECT DISTINCT <table>.last_name FROM <table> WHERE <table>.first_name LIKE 'M%'
将 <table>
替换为您的 table 姓名。