2016-08-22 125 views
-4

Iam嘗試連接兩個表(view_sales_report和com_payments)並顯示來自view_sales_report的所有記錄。查詢如下,但問題顯示只有com_payments條目的記錄。我想我需要改變哪裏的條件。加入兩個表並顯示所有記錄

SELECT 
view_sales_report.tender_id, 
view_sales_report.customer_code, 
view_sales_report.marketing_exe, 
com_payments.main_section, 
com_payments.amt_curr, 
com_payments.amount, 
com_payments.date_of_pay, 
com_payments.cat_paid, 
view_sales_report.category, 
view_sales_report.po_received_date 
FROM view_sales_report 
JOIN com_payments ON com_payments.tender_id = view_sales_report.tender_id AND com_payments.main_section ='Incentives' 
WHERE com_payments.cat_paid = 'Marketing' 
ORDER BY view_sales_report.file_no DESC 

任何人都可以幫助我嗎?

表view_sales_report

tender_id | customer_code | 
1   | 0    | 
2   | 0    | 
3   | 0    | 
4   | 0    | 
5   | 1    | 
6   | 0    | 

表Com_Payments

tender_id | main_section | cat_paid 
1   |    | marketing 

所需的輸出

tender_id | customer_code | cat_paid 
1   | 0    | Marketing 
2   | 0    | 
3   | 0    | 
4   | 0    | 
5   | 1    | 
6   | 0    | 

輸出IAM獲得

tender_id | customer_code | cat_paid 
1   | 0    | Marketing 
+0

顯示與預期結果 – Jens

+0

請查看一些示例數據,我已經添加所需的輸出和輸出IAM越來越 –

回答

0

使用左連接和其中添加到on條款

SELECT 
view_sales_report.tender_id, 
view_sales_report.customer_code, 
view_sales_report.marketing_exe, 
com_payments.main_section, 
com_payments.amt_curr, 
com_payments.amount, 
com_payments.date_of_pay, 
com_payments.cat_paid, 
view_sales_report.category, 
view_sales_report.po_received_date 
FROM view_sales_report 
LEFT JOIN com_payments ON com_payments.tender_id = view_sales_report.tender_id AND com_payments.main_section ='Incentives' 
AND com_payments.cat_paid = 'Marketing' 
ORDER BY view_sales_report.file_no DESC 
+0

太謝謝你了。工作正常。 –

相關問題