标题:【求助】如何合并三张不在同一数据库的表
只看楼主
yusinging
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-4
 问题点数:0 回复次数:4 
【求助】如何合并三张不在同一数据库的表
本菜坐标上海,再做一份美国数据库的实习。结果从MySQLWorkBench的连接开始就有很多问题了!目前这个程序最核心的问题是有三张不在一个数据库的表格需要合并,目前写出来的是
SELECT a.exchange,a.symbol,b.close, b.date, c.portdate FROM price_all as b, stock_list_morningstar as a, inst_holdings as c where b.sec=a.morn_secr_id and c.portdate=b.date;
感觉不太对,求大神指教。顺便求坐标上海的大神面基,只需要教教SQL和php就行,学费好商量,请吃饭。谢谢!
搜索更多相关主题的帖子: 数据库 上海 如何 美国 where 
2015-06-04 15:47
yusinging
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-6-4
得分:0 
附上我的php(跑不出)

<?php

//not on Saturday and Sunday
if(date("w",time())==0)exit;
if(date("w",time())==6)exit;

//chdir('/home/gurufocu/public_html/Vera');
//include "../include/config.php";
//include "../include/financial_functions.php";
//include_once ("../db_functions.php");        

//$db_stock = connect_stockdb();
//$db_data = connect_datadb();
//echo "\ntest\n";

$db_stock_dev = connect_stockdb();
$db_data_dev = connect_datadb_dev();

$date_today=date("Y-m-d");
echo "\n$date_today\n";
$date=date_create($date_today);
date_add($date,date_interval_create_from_date_string("+30 days"));
$date_new=date_format($date,"Y-m-d");
echo "\n$date_new\n";
//exit;

$sql="SELECT * FROM gurufocu_main.`inst_holdings` where filling_type in ('13D', 'SC 13D/A', 'SC 13D', '13D/A') and symbol!='' and portdate<'$date_new' and verified=1 order by portdate desc limit 0,1";
$result=mysql_query($sql, $db_stock_dev);

$nrows = mysql_num_rows($result);
echo "\n$nrows\n";

//loop one-to-one
        if($nrows!= 0){
                for($j1=0;$j1<$nrows;$j1++){
                        $row = mysql_fetch_assoc($result);
                    $symbol=$row['symbol'];
                        $exchange=$row['exchange'];
                        $guru_id=$row['guru_id'];
                        $portdate=$row['portdate'];
                        
//secr_id                     
            $sql_sec="SELECT a.morn_secr_id FROM gurufocu_main.stock_list as a where a.exchange='$exchange' and symbol='$symbol'";
                        echo $sql_price;
            $result_sec=mysql_query($sql_sec,$db_stock_dev);
            $nrows_sec = mysql_num_rows($result_sec);
                        
                if($nrows_sec != 0){
                            $row_sec = mysql_fetch_assoc($result_sec);
                        $morn_secr_id=$row_sec['morn_secr_id'];
                                        }
                        echo "\n$morn_secr_id\n";

$date_1=date_create($portdate);
date_add($date_1,date_interval_create_from_date_string("31 days"));
$date_30d=date_format($date_1,"Y-m-d");
                        
$sql_sec="SELECT b.close FROM gurufocu_data.price_all as b and gurufocu_main.stock_list as a where b.sec=a.morn_secr_id'";
                        echo $sql_price;
            $result_sec=mysql_query($sql_sec,$db_stock_dev);
            $nrows_sec = mysql_num_rows($result_sec);
                        
                if($nrows_sec != 0){
                            $row_sec = mysql_fetch_assoc($result_sec);
                        $close=$row_sec['close'];
                                        }
                        echo "\n$close\n";
                }
        }



?>
2015-06-04 15:49
wak4668
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:55
专家分:105
注 册:2012-8-20
得分:0 
b.sec=a.morn_secr_id、c.portdate=b.date中等号左右在数值上是对应关系么?sec能和id对应么?
2015-06-20 21:52
M小伟
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-9-2
得分:0 
sql语句?
2015-09-02 15:18
Maick
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:32
帖 子:251
专家分:1314
注 册:2012-9-21
得分:0 
不同库可以用数据库名区分啊.. 数据库名.表名.字段名
2015-09-09 11:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-446183-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064606 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved