2017-03-16 103 views
0

我創建了2個簡單表。 tableA和tableB,都只有1列'po'。 我正在嘗試使用FULL OUTER JOIN將2個表結合在一起。全外連接sql

錯誤輸出是警告:mysqli_fetch_array()期望參數1是mysqli_result。

所以我想我的查詢幾乎是錯的。感謝這裏的任何幫助!

<?php 
include 'db.php'; 

$sqlcount="SELECT DISTINCT po FROM testonly FULL OUTER JOIN testonly2 ON testonly.po=testonly2.po"; 

$resultcount = mysqli_query($conn,$sqlcount); 
while($row = mysqli_fetch_array($resultcount)){ 
echo $row['po']; 
} 
?> 

回答

1

沒有內置MySQL的FULL OUTER JOIN

SELECT * FROM testonly t1 
LEFT JOIN testonly2 t2 ON t1.po = t2.po 
UNION          -- use UNION ALL to retain common rows 
SELECT * FROM testonly t1 
RIGHT JOIN testonly2 t2 ON t1.po = t2.po 

PHP代碼:但是,你可以通過模擬查詢

$sqlcount = "SELECT * FROM testonly t1 LEFT JOIN testonly2 t2 ON t1.po = t2.po UNION ALL SELECT * FROM testonly t1 RIGHT JOIN testonly2 t2 ON t1.po = t2.po"; 

這裏閱讀更多信息:Full Outer Join in MySQL

+0

慣於此返回常見的行兩次? – jarlh

1

,如果你需要一個統一的表格,你需要和工會不能使用FULL OUTER JOIN,因爲這不是在mysql中可用的..但宥可以效仿和工會選擇

UNION子句意味着只有distint值合併兩個表..

 $sqlcount="SELECT po 
        FROM testonly 
        UNION 
        testonly2 "; 

工會都來自這兩個表意味着選擇所有行(也複製)的

 $sqlcount="SELECT po 
        FROM testonly 
        UNION ALL 
        testonly2 "; 

否則,如果你需要一個真正的連接(在同一行博特列),你可以根據你需要加入表

這是方式使用JOIN(內部連接..或左連接或右)內部連接

$sqlcount="SELECT DISTINCT t1.po, t2.po 
    FROM testonly t1 INNER JOIN testonly2 t2 ON testonly.po=testonly2.po";