我知道这个问题没有多大意义,但这里有:
Authority | Time
-------------------------------------
animuson@forums | 45.6758
132075829385895 | 49.7869
qykumsoy@forums | 44.45
439854390263565 | 50.761
user@forums | 44.9
another@auth | 46.123
bingo@nameo | 47.4392
让我解释一下。默认情况下,如果您尚未将帐户与您使用的权限相关联,则只会将时间存储为权限,但如果您关联帐户,则会存储您的ID号。我希望身份证号码的人具有优先权,因此他们会出现在没有联系但仍然有序的人身上。因此,对于这个数据样本,当选择前5个时,它将输出这些结果:
Authority | Time
-------------------------------------
qykumsoy@forums | 44.45
user@forums | 44.9
animuson@forums | 45.6758
132075829385895 | 49.7869
439854390263565 | 50.761
-------------------------------------
Ignoring These:
another@auth | 46.123
bingo@nameo | 47.4392
即使这两个用户有更好的时间,他们也被淘汰了,因为他们没有联系,链接的帐户被推高,但前5名仍然按时间顺序排列。可以安全地假设管理局内存在“@”符号表示它是一个未链接的帐户。它将始终显示在未链接的权限值中,并且链接的帐户将始终为纯数字。关于如何在一个查询中执行此操作的任何想法?
我使用的当前查询只是简单地选择前5个:
SELECT * FROM `tronner_times` WHERE `mid` = '{$map['mid']}' ORDER BY `time` + 0 LIMIT 5
答案 0 :(得分:3)
这是第一个想到的解决方案。我不确定它是否可以进一步优化,但您可能需要尝试以下方法:
SELECT dt.authority, dt.time
FROM (
SELECT authority, time
FROM tronner_times
ORDER BY INSTR(authority, '@') > 0, time
LIMIT 5
) dt
ORDER BY dt.time;
测试用例:
CREATE TABLE tronner_times (authority varchar(90), time decimal(8, 4));
INSERT INTO tronner_times VALUES ('animuson@forums', 45.6758);
INSERT INTO tronner_times VALUES ('132075829385895', 49.7869);
INSERT INTO tronner_times VALUES ('qykumsoy@forums', 44.45);
INSERT INTO tronner_times VALUES ('439854390263565', 50.761);
INSERT INTO tronner_times VALUES ('user@forums', 44.9);
INSERT INTO tronner_times VALUES ('another@auth', 46.123);
INSERT INTO tronner_times VALUES ('bingo@nameo ', 47.4392);
结果:
+-----------------+---------+
| authority | time |
+-----------------+---------+
| user@forums | 44.9000 |
| another@auth | 46.1230 |
| bingo@nameo | 47.4392 |
| 132075829385895 | 49.7869 |
| 439854390263565 | 50.7610 |
+-----------------+---------+
5 rows in set (0.00 sec)
我们要两次订购,因为派生表会返回最顶部没有@
符号的行。如果INSTR(authority, '@') > 0
字符串中存在1
,则表达式@
会返回authority
,如果不存在,则0
会返回time
。因此,结果集首先按此表达式排序,然后由@
字段排序,给予没有0
优先级的行(因为1
在time
之前排序)。因此,我们通过{{1}}字段对派生表中的5行进行排序,以生成预期的最终结果。
答案 1 :(得分:0)
我的想法是做一个案例陈述来过滤掉数字,因为你说它确认数字意味着相关联。我也注意到包含@forums的那些,所以这部分应该像%@ forums一样容易。显示了检查数字的示例链接,但您需要更改一下。第二个链接对我来说似乎更容易。
SELECT * FROM `tronner_times` WHERE PATINDEX('%[0-9]%',mid) > 0 OR mid like '%@forums' ORDER BY `time` + 0 LIMIT 5
http://www.tek-tips.com/faqs.cfm?fid=6423
http://www.sqlservercurry.com/2008/04/how-to-check-if-string-contains-numbers.html