2017-10-09 58 views
-3

我有兩個表名稱的收入和支出。我想要連接兩個表並獲取兩個日期範圍值之間的數據。如何加入兩個表之間的條件

Income     
=============   
id income_name  income_price income_date    
1 admission    30 2017-10-10 
2 hostel     40 2017-10-9 
3 bus     50 2017-10-7 

expenses     
============= 
id expenses_name  expenses_price expenses_date    
1 furniture    30 2017-10-9 
2 teacher     40 2017-10-8 
3 bus      60 2017-10-7 

我想從日期2017-10-6到2017-10-10之間的兩個表中檢索數據。請幫助我。 結果表
=============

id income_name  income_price  expense_name  expenses_price    
    1 admission   30    furniture  30 
    2 hostel   40     teacher  40 
    3 bus    50    bus   60 
+1

您可能需要聯合而不是聯接,但從您的問題中不清楚。也許你可以添加你的預期結果? –

+1

家庭作業問題需要努力尋求幫助。你嘗試過什麼嗎? –

+0

這可以幫助你:https://www.w3schools.com/sql/sql_join.asp你是指聯合兩張表嗎? – Ryosaku

回答

0

使用union all加入的結果集,你所得到的數據的2個表。以下是查詢。

select * 
from Income 
where date between '2017-10-6' and '2017-10-10' 
union all 
select * 
from expenses 
where date between '2017-10-6' and '2017-10-10'; 
+0

請避免代碼只有答案。總是添加一個簡短的描述。 – Alex

+0

不工作。我已經添加了結果表。請找到並給我解決方案 –

+0

請分享您的期望輸出。 –

2

您可以使用UNION運算符來合併兩個或多個查詢

Select * from income where (date between 'your_start_date' and 'your_end_date') 
UNION 
Select * from expenses where (date between 'your_start_date' and 'your_end_date') 

UNION運營商在默認情況下只選擇不同值的結果集。要允許重複的值,請使用UNION ALL

+0

聯盟不能正常工作 –

+0

你能告訴我你查詢了什麼嗎? –

+0

SELECT i.income_title,i.income_price,e.expenses_title,expenses_.price來自收入i LEFT JOIN費用e on i.income_date = e.expense_date WHERE i.income_date BETWEEN'2017-10-6'和'2017-10- 10' –