SELECT组两次

时间:2017-10-27 14:51:55

标签: mysql sql database request

我根本不擅长DB,我需要你的帮助。 我需要两次带有GROUP的SQL请求。

表格中的数据示例

<table border="1" style="border-collapse:collapse">
<tr><th>id</th><th>market_id</th><th>price</th><th>low</th><th>high</th><th>symbol</th><th>created_at</th></tr>
<tr><td>1</td><td>1</td><td>5773.8</td><td>5685</td><td>6020</td><td>btcusd</td><td>2017-10-27 16:46:10</td></tr>
<tr><td>2</td><td>1</td><td>0.4274</td><td>0.39</td><td>0.43983</td><td>iotusd</td><td>2017-10-27 16:46:11</td></tr>
<tr><td>3</td><td>1</td><td>0.20026</td><td>0.1986</td><td>0.20352</td><td>xrpusd</td><td>2017-10-27 16:46:12</td></tr>
<tr><td>4</td><td>2</td><td>5771</td><td>5685</td><td>6020</td><td>btcusd</td><td>2017-10-27 16:46:18</td></tr>
<tr><td>5</td><td>2</td><td>0.4274</td><td>0.39</td><td>0.43983</td><td>iotusd</td><td>2017-10-27 16:46:18</td></tr>
<tr><td>6</td><td>2</td><td>0.20026</td><td>0.1986</td><td>0.20352</td><td>xrpusd</td><td>2017-10-27 16:46:19</td></tr>
<tr><td>7</td><td>1</td><td>5773.1</td><td>5685</td><td>6020</td><td>btcusd</td><td>2017-10-27 16:46:25</td></tr>
<tr><td>8</td><td>1</td><td>0.4274</td><td>0.39</td><td>0.43983</td><td>iotusd</td><td>2017-10-27 16:46:25</td></tr>
<tr><td>9</td><td>1</td><td>0.20026</td><td>0.1986</td><td>0.20352</td><td>xrpusd</td><td>2017-10-27 16:46:26</td></tr>
<tr><td>10</td><td>2</td><td>5773.1</td><td>5685</td><td>6020</td><td>btcusd</td><td>2017-10-27 16:46:32</td></tr>
<tr><td>11</td><td>2</td><td>0.42741</td><td>0.39</td><td>0.43983</td><td>iotusd</td><td>2017-10-27 16:46:32</td></tr>
<tr><td>12</td><td>2</td><td>0.20026</td><td>0.1986</td><td>0.20352</td><td>xrpusd</td><td>2017-10-27 16:46:33</td></tr></table>

我想获取每个market_id和符号的最新数据 这意味着我最终需要这样的事情:

 - id  market_id symbol
 - 7    1        btcusd
 - 8    1        iotusd
 - 9    1        xrpusd
 - 10   2        btcusd
 - 11   2        iotusd
 - 12   2        xrpusd

真的需要帮助,有点受阻。

4 个答案:

答案 0 :(得分:1)

沿着这些方向......

SELECT MAX(id), market_id, symbol
    FROM crypto
    GROUP BY market_id, symbol

答案 1 :(得分:1)

这里我的评论称为SQL。

SELECT A.ID, A.MarketID, A.Symbol, A.Price, A.Low, A.High
FROM CRYPTO A
INNER JOIN (SELECT max(Created_at) MCA, Market_ID, Symbol
            FROM crypto
            GROUP BY Market_ID, Symbol) B
  on A.Created_At = B.MCA
 and A.market_ID = B.Market_ID
 and A.Symbol = B.Symbol

这是做什么的:

派生表(别名B)为每个market_ID和具有max created_at时间的符号生成1行。然后,它使用此派生表集连接回基集(别名A),以将数据限制为具有max created_at的数据。这允许我们为每个独特的market_Id和符号显示A的整个记录​​;但仅适用于具有max created_at的记录。

其他引擎允许您使用交叉应用或分析来获得所需的结果。

答案 2 :(得分:1)

你快到了。试试这个

SELECT c.* 
FROM CRYPTO as C
JOIN (
  SELECT market_id, symbol, MAX(id) as maxid
  FROM CRYPTO
  GROUP BY market_id, symbol
) AS C2
ON   C2.maxid = C.id and C.market_id = c2.market_id and c.symbol = c2.symbol

答案 3 :(得分:0)

我尝试了这些请求

SELECT * FROM CRYPTO as C3
  JOIN (
     SELECT MAX(id) as max
     FROM CRYPTO as C1
     GROUP BY symbol
   ) AS C2
ON C2.max = C3.id

SELECT M.id, M.name, R.symbol FROM MARKET AS M
  JOIN (
     SELECT DISTINCT C.symbol, C.market_id
     FROM CRYPTO as C
   ) as R
ON M.id = R.market_id

但最后我没有找到好的组合。