我在用户对象中有一个ManyToManyField,它用于映射用户关注的用户.我正在尝试显示他们最近关注的人的子集列表.在.order_by()中是否有一个技巧可以让我按ManyToManyField的id排序?数据在那里,对吗?
# (people the user is following)
following = models.ManyToManyField(User, related_name="following", blank=True)
theuser.following.filter(user__is_active=True).order_by("user__id")
Run Code Online (Sandbox Code Playgroud)
这将为我提供用户所关注的用户列表,但在他们加入时按订单排序.我希望以下列表的顺序按用户跟随它们的顺序排列.
我已经创建了一个sql视图,我需要在4个字段上使用ORDER BY对select的结果进行排序,但是我收到的消息是ORDER BY不能在视图中使用,除非我使用TOP.有人可以解释为什么需要TOP,有人有一个解决方法在sql视图中进行排序吗?
谢谢.
我有一个为客户提供的模型以及他们的购买/订单模型.在我们的后端/网络管理员中,我们希望能够按最近的订单对客户列表进行排序.
这基本上就是我们模型的样子
class Customer(models.Model):
username = models.CharField(max_length=32)
first_name = models.CharField(max_length=322)
def __unicode__(self):
return "%s" % self.username
def get_last_order_date(self):
self.order_set.latest('purchase_date')
class Order(models.Model):
customer = models.ForeignKey(Customer)
purchase_date = models.DateTimeField()
Run Code Online (Sandbox Code Playgroud)
我可以使用我的get_last_order_date函数显示每个客户的最新订单,但是我不能按该函数排序(或者至少我不知道如何).我已经尝试了我能想到的一切:
.order_by('order__purchase_date')
.order_by('order__set').latest('purchase_date')
Run Code Online (Sandbox Code Playgroud)
还有更多,没有运气.
任何提示或线索将非常感激.
谢谢.
例如我在表EMPLOYEE中:
(code, name)
(1, 'Jimmy')
(2, 'Albert')
(3, 'Michelle')
(4, 'Felix' )
Run Code Online (Sandbox Code Playgroud)
如果您这样做:(从EMPLOYEE中选择*)您将得到:
(1, 'Jimmy')
(2, 'Albert')
(3, 'Michelle')
(4, 'Felix' )
Run Code Online (Sandbox Code Playgroud)
如果您这样做:(从EMPLOYEE中选择*,您将获得(1,3,2,4)中的代码:
(1, 'Jimmy')
(2, 'Albert')
(3, 'Michelle')
(4, 'Felix' )
Run Code Online (Sandbox Code Playgroud)
如何按IN子句中的CSV值顺序获取它,原样如何?
(1, 'Jimmy')
(3, 'Michelle')
(2, 'Albert')
(4, 'Felix' )
Run Code Online (Sandbox Code Playgroud) 例如,我有这个表:
itemgroup | 描述| 价格
A,a,10
A,b,12
A,c,14
B,g,11
B,h,16
现在我想在一个组中选择价格最高的行,如下所示:
A,c,14
B,h,16
SQL查询(功能齐全)让我接近这个:
SELECT itemgroup, MAX( price )
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)
A,14
B,16
通过尝试这个我得到一个"不是GROUP BY表达式"-error:
SELECT itemgroup, description, MAX( price )
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)
我需要像这样的伪查询:
SELECT itemgroup, IGNORE( description), MAX( price )
FROM table
GROUP BY itemgroup
Run Code Online (Sandbox Code Playgroud)
我希望我能解释一下我的小问题.
我需要做的GROUP BY之后ORDER BY.我不明白为什么MySQL不支持它.这是我的代码:
SELECT
`pages`.`id`,
`contents`.`id_language`,
[...]
[...]
ORDER BY
FIND_IN_SET(`languages`.`id`, '3') DESC
[the GROUP BY]
Run Code Online (Sandbox Code Playgroud)
结果将是这样的:
id | id_language | ...
1 3
1 1
2 3
2 5
2 1
Run Code Online (Sandbox Code Playgroud)
我需要按ID分组,我只需要第一个结果,我需要保存在视图中.因此,我无法使用SUBQUERY.
结果必须是:
id | id_language | ...
1 3
2 3
Run Code Online (Sandbox Code Playgroud)
注意:不要混淆id_language = 3,因为它不是一个规则.
在查询中引入ORDER BY子句会增加总时间,因为db必须执行额外的工作才能对结果集进行排序:
我想念的是为什么只从连接表中添加一列产生如此不同的性能.
EXPLAIN ANALYZE
SELECT p.*
FROM product_product p
JOIN django_site d ON (p.site_id = d.id)
WHERE (p.active = true AND p.site_id = 1 )
ORDER BY d.domain, p.ordering, p.name
Run Code Online (Sandbox Code Playgroud)
Sort (cost=3909.83..3952.21 rows=16954 width=1086) (actual time=1120.618..1143.922 rows=16946 loops=1)
Sort Key: django_site.domain, product_product.ordering, product_product.name
Sort Method: quicksort Memory: 25517kB
-> Nested Loop (cost=0.00..2718.86 rows=16954 width=1086) (actual time=0.053..87.396 rows=16946 loops=1)
-> Seq Scan on django_site (cost=0.00..1.01 rows=1 width=24) (actual time=0.010..0.012 rows=1 loops=1)
Filter: (id = 1) …Run Code Online (Sandbox Code Playgroud) 我的查询
select * from product where productId in(25,36,40,1,50);
Run Code Online (Sandbox Code Playgroud)
结果如下
`productId ProductName Qty Price`
-------------------------------------
`1 | namesome | 5 | 25.00`
`25 | namesome | 5 | 35.00`
`36 | namesome | 5 | 35.00`
`40 | namesome | 5 | 35.00`
`50 | namesome | 5 | 35.00`
Run Code Online (Sandbox Code Playgroud)
我没有使用任何order by子句,但它自动应用的顺序productId,
我需要任何类型的结果,如下所示
`productId ProductName Qty Price`
-------------------------------------
`25 | namesome | 5 | 25.00`
`36 | namesome | 5 | 35.00`
`40 | namesome | 5 …Run Code Online (Sandbox Code Playgroud) 我想在最高分上添加积分.我的桌子是

我试图给出学生最高3分的积分.第一名最高分为5分,第二高分为3分,第三高分为1分
我使用此代码选择最高标记,
select t1.ID, t1.Name, t1.Section, t1.Marks from myTable t1 join
(select Section, substring_index(group_concat
(distinct Marks order by Marks desc), ',', 3) as Marks3
from myTable group by Section ) tsum on t1.Section = tsum.Section
and find_in_set(t1.Marks, tsum.Marks3) > 0 ORDER BY Section, Marks DESC, ID Desc
Run Code Online (Sandbox Code Playgroud)
我想为第一高分值增加5分,第二高分为3分,第三高分为1分.学生可能会出现重复标记.
请参阅http://www.sqlfiddle.com/#!2/dca0c/1
所以我的最终输出

请帮我..
sql-order-by ×10
sql ×6
mysql ×5
django ×2
collation ×1
group-by ×1
oracle10g ×1
performance ×1
postgresql ×1
python ×1
select ×1
sorting ×1
view ×1