我想运行此查询:
SELECT DISTINCT ON (address_id) purchases.address_id, purchases.*
FROM purchases
WHERE purchases.product_id = 1
ORDER BY purchases.purchased_at DESC
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
PG ::错误:错误:SELECT DISTINCT ON表达式必须与初始ORDER BY表达式匹配
添加address_id为第一个ORDER BY表达式会使错误无效,但我真的不想添加排序address_id.是否可以不通过订购address_id?
我怎么能在django的查询集中有一个子查询?例如,如果我有:
select name, age from person, employee where person.id = employee.id and
employee.id in (select id from employee where employee.company = 'Private')
Run Code Online (Sandbox Code Playgroud)
这就是我所做的.
Person.objects.value('name', 'age')
Employee.objects.filter(company='Private')
Run Code Online (Sandbox Code Playgroud)
但它没有工作,因为它返回两个输出...