【发布时间】:2020-08-01 09:14:15
【问题描述】:
我使用以下方法按每个域组的最高分对数据进行排序:
SELECT * from `Inputs` t
order by (select max(`score`) from `Inputs` t1 where t1.`domain`=t.`domain`) desc, `score` desc
结果是:
query domain url score
a www.google.com www.google.com/a 3
a www.google.com www.google.com/b 1
a www.facebook.com www.google.com/c 2
我不想将域和 url 存储在数据库中,而是使用以下方法计算域作为查询中 url 的函数:
SUBSTRING(`url` from 1 for locate('/',`url` ,10)-1) as `domain`
如果可能,我将能够使用更少的列来实现相同的条目顺序(如上所示):
query url score
a www.google.com/a 3
a www.google.com/b 1
a www.google.com/c 2
如果可能的话,有谁知道这是如何实现的?我没有在 stackoverflow 上找到其他关于使用临时表的列别名的问题。
我尝试嵌套另一个查询,但没有运气:
SELECT *, SUBSTRING(`url` from 1 for locate('/',`url` ,10)-1) as `domain` from `Inputs` t
order by (select max(`score`),
(select SUBSTRING(`url` from 1 for locate('/',`url` ,10)-1) as `domain` from `Inputs` t1 )
from `Inputs` t1 where t1.`domain`=t.`domain`) asc, `score` asc
【问题讨论】:
-
你真的不止一次地问同样的问题,并且接受(有时)一个答案,在被问到之后改变问题,并且有人要求更多信息,就开始无视他吗? (参考1:stackoverflow.com/questions/63202363/…;参考2:stackoverflow.com/questions/63193038/…)
标签: php mysql phpmyadmin subquery sql-order-by