如何加入两个表之间的条件

问题描述:

我有两个表名称的收入和支出。我想要连接两个表并获取两个日期范围值之间的数据。如何加入两个表之间的条件

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

使用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

请分享您的期望输出。 –

您可以使用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' –