嵌套查询(基于同一个表)

时间:2016-01-18 01:59:35

标签: right-join

我在S&P500指数上有每个股票交易的历史股票价格表。

每个工作日,500条新记录都会插入historical_prices表中:

  

symbol,close_date,close_price。

该过程经常因多种原因而失败,只有一小部分记录被添加。

周四正常运行(500条新记录),周五失败(仅添加400条记录)。

我需要确定剩余的100条未添加的记录,以便重新运行并完成整个过程。

嵌套查询是否是最有效的方法?从星期四选择500条记录,并根据星期五的400条记录进行衡量)

SELECT * FROM historical_prices 
WHERE `close_date` = '2016-01-16'

RIGHT JOIN (
   SELECT * FROM historical_prices 
   WHERE `close_date` = '2016-01-15')
WHERE `symbol` IS NULL;

先谢谢!

2 个答案:

答案 0 :(得分:0)

你可以这样做:

<?php
//300 seconds = 5 minutes - or however long you need so php won't time out
ini_set('max_execution_time', 300); 

// using a global to store the links in case there is recursion, it makes it easy. 
// You could of course pass the array by reference for cleaner code.
$alinks = array();

// set the link to whatever you are reading
$link = "http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml";

// do the search
linksearch($link, $alinks);

// show results
var_dump($alinks);

function linksearch($url, & $alinks) {
    // use $queue if you want this fn to be recursive
    $queue = array();
    echo "<br>Searching: $url";

    $href = array();
    //Load the HTML page
    $html = file_get_contents($url);

    //Create a new DOM document
    $dom = new DOMDocument;

    //Parse the HTML. The @ is used to suppress any parsing errors
    //that will be thrown if the $html string isn't valid XHTML.
    @$dom->loadHTML($html);

    //Get all links. You could also use any other tag name here,
    //like 'img' or 'table', to extract other tags.
    $links = $dom->getElementsByTagName('link');

    //Iterate over the extracted links and display their URLs
    foreach ($links as $link){

        //Extract and show the "href" attribute. 
        $href[] = $link->getAttribute('href');
    }    
    foreach (array_unique($href) as $link) {            
        // add to list of links found
        $queue[] = $link;
    }

    // remove duplicates
    $queue = array_unique($queue);

    // get links that haven't yet been processed
    $queue = array_diff($queue, $alinks);

    // update array passed by reference with new links found
    $alinks = array_merge($alinks, $queue);

    if (count($queue) > 0) {
        foreach ($queue as $link) {
            // recursive search - uncomment out if you use this
            // remember to check that the domain is the same as the one starting from
            // linksearch($link, $alinks);
        }
    }
}

答案 1 :(得分:0)

在这种情况下,子查询SELECT * FROM historical_prices h1 WHERE h1.`close_date` = '2016-01-16' AND NOT EXISTS ( SELECT 1 FROM historical_prices h2 WHERE h2.`close_date` = '2016-01-15' and h2.`symbol = h1.`symbol` ) 将很容易理解。子查询Java Application会快一点。

{{1}}