我正在使用这个SQL查询。查询几乎是正确的,我只需要将ticktimes作为表中的最新条目。对不是查询返回每个CurrencyID
中最旧的行+------------+------------+---------------------+------------+---------------+
| CurrencyID | MarketName | TickTime | Last | Volume |
+------------+------------+---------------------+------------+---------------+
| 1 | BTC-LTC | 2017-08-25 19:54:04 | 0.01169000 | 2502.10724529 |
| 2 | BTC-VTC | 2017-08-25 19:54:05 | 0.00021742 | 2791.78612635 |
| 3 | BTC-DASH | 2017-08-25 19:54:05 | 0.07104090 | 660.87899945 |
| 4 | BTC-XMR | 2017-08-25 19:54:06 | 0.02189143 | 3682.28908088 |
| 5 | BTC-CANN | 2017-08-25 19:54:06 | 0.00001145 | 1447.45006154 |
...
...
| 20 | BTC-BCC | 2017-08-25 19:54:14 | 0.14220000 | 5196.84118604 |
+------------+------------+---------------------+------------+---------------+
我现在正在处理的查询看起来像这样。这可能是一个简单的问题,我的sql只是生锈嘿嘿
SELECT
c.CurrencyID,
c.MarketName,
sub_table.TickTime,
sub_table.Last,
sub_table.Volume
FROM
(SELECT
CurrencyID,
TickTime,
Last,
Volume,
@rn:=CASE
WHEN @var_CurrencyID = CurrencyID THEN @rn + 1
ELSE 1
END AS rn,
@var_CurrencyID:=CurrencyID
FROM
(SELECT @var_CurrencyID:=NULL, @rn:=NULL) vars, Ticker
WHERE
CurrencyID IN (SELECT CurrencyID FROM Currency)
ORDER BY CurrencyID, TickTime DESC) as sub_table
INNER JOIN Currency c
on c.CurrencyID=sub_table.CurrencyID
WHERE
rn <= 1
ORDER BY CurrencyID , TickTime DESC;
输出来自这两个表:
Currency
+------------+--------------+------------------+------------+----------+--------------+------------+
| CurrencyID | CurrencyName | CurrencyNameLong | MarketName | IsActive | MinTradeSize | TxFee |
+------------+--------------+------------------+------------+----------+--------------+------------+
Ticker
+----------+------------+---------------------+------------+------------+------------+---------------+
| TickerID | CurrencyID | TickTime | Bid | Ask | Last | Volume |
+----------+------------+---------------------+------------+------------+------------+---------------+
修改 为了使事情更清楚:我希望TickTime成为表中插入的最新记录。这是我希望输出如何的例子:
(将更改&lt; = 1以列出更多记录......)
+------------+------------+---------------------+------------+---------------+
| CurrencyID | MarketName | TickTime | Last | Volume |
+------------+------------+---------------------+------------+---------------+
| 1 | BTC-LTC | 2017-08-28 16:40:00 | 0.01169000 | 2499.55344592 |
| 1 | BTC-LTC | 2017-08-28 17:40:04 | 0.01169000 | 2502.10724529 |
| 2 | BTC-VTC | 2017-08-28 16:40:01 | 0.00021742 | 2791.78613576 |
| 2 | BTC-VTC | 2017-08-28 17:40:05 | 0.00021742 | 2791.78612635 |
| 3 | BTC-DASH | 2017-08-28 16:40:02 | 0.07104060 | 660.88002045 |
| 3 | BTC-DASH | 2017-08-28 17:40:05 | 0.07104090 | 660.87899945 |
| 4 | BTC-XMR | 2017-08-28 16:40:02 | 0.02178855 | 3681.62291540 |
| 4 | BTC-XMR | 2017-08-28 17:40:06 | 0.02189143 | 3682.28908088 |
| 5 | BTC-CANN | 2017-08-28 16:40:03 | 0.00001154 | 1449.06053986 |
| 5 | BTC-CANN | 2017-08-28 17:40:06 | 0.00001145 | 1447.45006154 |
...
...
| 20 | BTC-BCC | 2017-08-28 16:40:10 | 0.14265409 | 5196.39621496 |
| 20 | BTC-BCC | 2017-08-28 17:40:14 | 0.14220000 | 5196.84118604 |
+------------+------------+---------------------+------------+---------------+