简体   繁体   中英

Using Join and Between to Link Tables and Find Values

I am trying to join these two tables and get a result, keep all results from Table 1 and find the value from Table 2 (return NULL if nothing is found in Table 2 matching criteria)

TABLE1

|ROUTE|MEASURE|
|:---:|:-----:|
|1|6|
|1|12|
|2|1|
|3|2|
|3|5|

TABLE2

|ROUTE|BEGINMEASURE|ENDMEASURE|VALUE|
|:---:|:-----:|:--:|:--:|
|1|0|5|A|
|1|5|10|B|
|2|0|5|C|
|3|0|3|D|
|3|3|10|E|
|3|10|12|F|

My code is:

select t1.route, t1.measure, t2.value  
from table1 t  
left join table2 t2 on t1.route=t2.route  
where t1.route=t2.route and t1.measure between t2.beginmeasure and t2.endmeasure;   

Add the condition to the join:

select t1.route, t1.measure, t2.value  
from table1 t  
left join table2 t2 on t1.route=t2.route and t1.measure between t2.beginmeasure and t2.endmeasure;   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM