简体   繁体   中英

Between variables sql

I've been thinking about this but didn't get a solution yet.

I have 2 tables

TABLE1

FrID FrCode Zstart  ZEnd
-------------------------
1      AQ   10000   20000
2      AW   34578   67459
3      SR   86428   89758

TABLE2

ID NAME ZIP
-------------
1  XXX  35864
2  CCC  25758
3  FFF  87526

I need to get the frCode if the ZIP is between Zstart and ZEnd .

The problem is that I don't have a primary key to joint the tables. I know a between clause can help but not sure how to do for all values.

Note: Id and FrID are not key values

Between is a common SQL construct to use for querying between date ranges so to get frCode between start and end you can do something like below

SELECT t1.FrCode
FROM TABLE1 t1
JOIN TABLE2 t2 ON t1.FrID = t2.ID
WHERE t2.ZIP BETWEEN t1.Zstart AND t1.ZEnd

If the FrID and ID are not related, then there must be some relation between Table1 and Table2 right if you want to get FrCode then you will need to join on that relation

ON t1.FrID = t2.OtherColumn

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