2012-04-07 9 views
0

1つのクエリで取得したい次の2つのクエリがあります。私はしようとしましたが、それは起こりませんでした。親切に私を助けてくれますか?事前に2つのmysqlクエリをまとめて結合する

感謝:)

クエリ:1

" SELECT * FROM vouchers 
LEFT JOIN details on vouchers.voucher_no = details.voucher_no 
LEFT JOIN accounts on accounts.code = details.t_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001')" 

問合せ:2

"SELECT * FROM details 
LEFT JOIN vouchers on details.voucher_no = vouchers.voucher_no 
LEFT JOIN accounts on accounts.code = vouchers.account_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001')" 

私は次のことを試してみましたが、私は私がエラーを持っているというエラーメッセージが表示されました何SQL構文。

$getData = $this->db->query("SELECT * FROM vouchers 
LEFT JOIN details on vouchers.voucher_no = details.voucher_no 
LEFT JOIN accounts on accounts.code = details.t_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001'), <<I just separated with a comma 

SELECT * FROM details 
LEFT JOIN vouchers on details.voucher_no = vouchers.voucher_no 
LEFT JOIN accounts on accounts.code = vouchers.account_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001')"); 

if($getData->num_rows() > 0) 
return $getData->result_array(); 
else 
return null; 
} 
+0

どのようにあなたがそれらに参加したいんUNIONとそれらを接続しますか? INNER JOINについて話していますか?どんな分野に参加したいですか? –

+0

詳細とバウチャー間で外部結合が必要なように見えます。おそらく単一のクエリとして書き直すことができます(たとえば、SELECT * FROM詳細、バウチャー...)。ただし、そうでない場合は、常にUNION ALLがあります。 – cmbuckley

答えて

1

ALL

$getData = $this->db->query("SELECT * FROM vouchers 
LEFT JOIN details on vouchers.voucher_no = details.voucher_no 
LEFT JOIN accounts on accounts.code = details.t_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001') 

UNION ALL 

SELECT * FROM details 
LEFT JOIN vouchers on details.voucher_no = vouchers.voucher_no 
LEFT JOIN accounts on accounts.code = vouchers.account_code 
    WHERE (voucher_type='1' AND account_code='1001') 
     OR (voucher_type='0' AND t_code='1001') 
     OR (voucher_type='0' AND account_code='1001')"); 
+0

ありがとうCichy :) –

関連する問題