我搜索了论坛,找到了我的问题的一些解决方案,我把它放在一起进行MySQL查询.
我希望按顺序得到一匹马的行号,这样我才能看到它在订单中的位置.
我使用了以下代码
SELECT `odds`, (SELECT COUNT(*) FROM `tom_cards` WHERE `Racetime` = "14:30" Order By `Odds`) AS `position`, `Horse` FROM `tom_cards` WHERE `Racetime` = "14:30"
把它创造所有的位置为8
> odds position Horse
> 100.00 8 Central School
> 1.25 8 Coologue
> 1.10 8 Ma du Fou
> 33.00 8 Quintano
> 66.00 8 The Mobb
> 12.00 8 The Western Force
> 22.00 8 Youngdocgallagher
> 8.00 8 Tara Mac
我希望它是从最低到最高的赔率顺序如下:
odds position Horse
100.00 8 Central School
1.25 2 Coologue
1.10 1 Ma du Fou
33.00 6 Quintano
66.00 7 The Mobb
12.00 4 The Western Force
22.00 5 Youngdocgallagher
8.00 3 Tara Mac
有人可以帮忙吗?
解决方法:
您可以使用相关子查询执行所需操作:
SELECT `odds`,
(SELECT COUNT(*)
FROM `tom_cards` tc2
WHERE tc2.Racetime = tc.RaceTime and tc2.Odds <= tc.Odds
) AS `position`, `Horse`
FROM `tom_cards` tc
WHERE `Racetime` = '14:30';
我猜你如果你有重复赔率,那么你希望他们拥有相同的“位置”.如果是这样,你需要等效的rank()(这是该操作的ANSI标准函数,但MySQL不支持它):
SELECT `odds`,
(SELECT 1 + COUNT(*)
FROM `tom_cards` tc2
WHERE tc2.Racetime = tc.RaceTime and tc2.Odds < tc.Odds
) AS `position`, `Horse`
FROM `tom_cards` tc
WHERE `Racetime` = '14:30';
标签:php,mysql,mysqli
来源: https://codeday.me/bug/20190830/1766428.html