2012-07-15 53 views
2
select t1.CardID,t2.Description,t5.BioData 
from db2.tblemployeeinfob t1 
left join (db2.tbldepartments t2,db1.tblbiometrics t5) 
on 
(t1.Department = t2.DepartmentID and 
t1.CardID=t5.CardID 
) 

返回結果是1420 | (NULL) | (NULL)MySQL查詢跨數據庫加入返回NULL

預期結果爲:1420 | DB2_Description_Value | DB1_BioData_value

如果我刪除跨數據庫連接,等清除DB1然後查詢將正常工作,加入從剩下的兩個表同一數據庫。

如果我做DB1和DB2之間的交叉數據庫連接,即使是從同一個數據庫DB2表T2將返回NULL。

哪裏是我的查詢問題,所以我可以從兩個數據庫中獲得價值。

+0

一件事ü可以做的是前綴DBNAME太與表名 – nawfal 2012-07-15 21:44:19

回答

2

你不應該在這裏使用交叉連接。你希望兩個獨立的左聯接:

SELECT t1.CardID, t2.Description, t5.BioData 
FROM db2.tblemployeeinfob AS t1 
LEFT JOIN db2.tbldepartments AS t2 ON t1.Department = t2.DepartmentID 
LEFT JOIN db1.tblbiometrics AS t5 ON t1.CardID = t5.CardID 
+0

一個問題就解決了一起,現在我可以從同一個數據庫,但價值從第二表值db1仍爲NULL – 2012-07-15 21:50:02

+0

@Wasim:這是因爲該值不存在於db1中。 – 2012-07-15 21:50:53

+0

正確,你能解釋爲什麼我不能在跨數據庫中使用單一的LEFT JOIN – 2012-07-15 21:59:44

0
  <?php 
      include('../dbcon.php'); 
      include('../session.php'); 
      if (isset($_POST['delete_user'])){ 
      $id=$_POST['selector']; 
      $class_id = $_POST['teacher_class_id']; 
      $get_id=$_POST['get_id']; 
      $N = count($id); 
      for($i=0; $i < $N; $i++) 
      { 
       $result = mysql_query("select * from files where file_id = '$id[$i]' ")or die(mysql_error()); 
       while($row = mysql_fetch_array($result)){ 

       $fname = $row['fname']; 
       $floc = $row['floc']; 
       $fdesc = $row['fdesc']; 
       $uploaded_by = $row['uploaded_by']; 



       mysql_query("insert into files (floc,fdatein,fdesc,class_id,fname,uploaded_by) value('$floc',NOW(),'$fdesc','$class_id','$fname','$uploaded_by')")or die(mysql_error()); 


       } 
      } 
      ?> 
      <script> 
      window.location = 'downloadable.php<?php echo '?id='.$get_id; ?>'; 
      </script> 
      <?php 
      } 

      if (isset($_POST['copy'])){ 
      $id=$_POST['selector']; 

      $N = count($id); 
      for($i=0; $i < $N; $i++) 
      { 
       $result = mysql_query("select * from files where file_id = '$id[$i]' ")or die(mysql_error()); 
       while($row = mysql_fetch_array($result)){ 


        $fname = $row['fname']; 
       $floc = $row['floc']; 
       $fdesc = $row['fdesc']; 


       mysql_query("insert into teacher_backpack (floc,fdatein,fdesc,teacher_id,fname) value('$floc',NOW(),'$fdesc','$session_id','$fname')")or die(mysql_error()); 


       } 
      } 
      ?> 
      <script> 
      window.location = 'backpack.php'; 
      </script> 
      <?php 
      } 
      ?> 
      <?php 

      if (isset($_POST['share'])){ 
      $id=$_POST['selector']; 
      $teacher_id = $_POST['teacher_id1']; 
      echo $teacher_id ; 
      $N = count($id); 
      for($i=0; $i < $N; $i++) 
      { 
       $result = mysql_query("select * from files where file_id = '$id[$i]' ")or die(mysql_error()); 
       while($row = mysql_fetch_array($result)){ 


        $fname = $row['fname']; 
       $floc = $row['floc']; 
       $fdesc = $row['fdesc']; 


       mysql_query("insert into teacher_shared (floc,fdatein,fdesc,teacher_id,fname,shared_teacher_id) value('$floc',NOW(),'$fdesc','$session_id','$fname','$teacher_id')")or die(mysql_error()); 


       } 
      } 
      ?> 
      <script> 
      window.location = 'tambah_share_file.php'; 
      </script> 
      <?php 
      } 
      ?>