0

1 つのクエリで取得したい次の 2 つのクエリがあります。やってみたのですがうまくいきませんでした、どなたか教えていただけないでしょうか?

前もって感謝します :)

クエリ: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;
}
4

1 に答える 1

1

それらを UNION 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')");
于 2012-04-07T15:09:11.057 に答える