簡體   English   中英

如何用where條件連接兩個表

[英]How to join two table with where between condition

我有兩個表名收入和支出。 我想連接兩個表並獲取兩個日期范圍值之間的數據。

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 

可以使用聯合運算符合並兩個或多個查詢的結果集

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM