如何在MYSQL

时间:2017-12-26 19:41:36

标签: php mysql

我有两个相互依赖的MYSQL基本查询,这是我的quires

@$query = "SELECT * FROM coins_tokens";
$row    = $db->Execute($query);

foreach ($row as $rowItem) {
    $name = $rowItem['ct_id'];

    @$sql1 = "SELECT * FROM historical_data  WHERE `name` = '".$name."' GROUP BY name LIMIT  30";
    $row2 = $db->Execute($sql1);
    foreach ($row2 as $rowItem2){ 
        $market_cap     = $rowItem2['market_cap'];
        if($market_cap >= 500000000){

        }
    }
}

它减慢了我的整个过程并花费了大量时间来执行,因为在coins_tokens中有超过1400个结果,然后在表ct_id中再次有超过600000个记录第一个表而name是conman。

我想要做的是在过去7天内获得超过5亿market_cap的货币。所以我从第一张表中获取货币,从第二张表中获取历史数据,并检查过去7天内market_cap是否有所增加。

以下是historical_data表的结构和数据:

enter image description here enter image description here

1 个答案:

答案 0 :(得分:-1)

SELECT
    c.*,
    d.`date`,
    d.market_cap
FROM coins_tokens AS c
    LEFT JOIN historical_data AS d ON c.ct_id = d.name
WHERE d.market_cap >= '$mketcapgrter'
  AND DATE(d.`date`) >= CURRENT_DATE() - INTERVAL 30 DAY 
GROUP BY d.name 
ORDER BY d.market_cap DESC LIMIT 100