你应该像下面这样重新设计你的桌子
CountryTable : CountryID (int, autonumber, PK), CountryName (varchar2)
StateTable : StateID (int, auto number, PK), StateName (varchar2), ContryID (int , FK)
CityTable : CityID (int, auto number, PK), CityName (varchar2), StateID (int, FK)
而不是使用这三个 promoScheduleCountry、promoScheduleStates、promoScheduleCities 表,而是像这样创建一个新表
PromoLocation : PromoLocationId (int, autonumber, PK), PromoScheduleID (int, FK), CountryID(int, FK), StateID(int, FK), CityID(int, FK)
所有表中的虚拟数据就像
CountryTable
---------------------------
CountryID | CountryName
---------------------------
1 | USA
2 | France
3 | Russia
4 | India
---------------------------
StateTable
-----------------------------------------
StateID | StateName | CountryID
-----------------------------------------
1 | New York | 1
2 | Alaska | 1
3 | Michigan | 1
-----------------------------------------
CityTable
-----------------------------------------
CityID | CityName | StateID
-----------------------------------------
1 | Albany | 1
2 | Buffalo | 1
3 | Rochester | 1
-----------------------------------------
PromoSchedule
-----------------------------------------
promoScheduleid | PromoEventName
-----------------------------------------
1 | Event1
2 | Event2
3 | Event3
4 | Event4
----------------------------------------
PromoLocation
--------------------------------------------------------------------------------------
PromoLocationID | PromoScheduleID | CountryID | StateID | CityID
--------------------------------------------------------------------------------------
1 | 1 | 1 | |
2 | 2 | | 1 |
3 | 3 | | | 1
4 | 4 | | | 2
---------------------------------------------------------------------------------------
你的查询就像
SELECT * FROM PromoSchedule WHERE promoScheduleid IN
(SELECT promoScheduleid FROM PromoLocation WHERE CountryID = '<CountryID>' OR
StateID IN (SELECT StateID FROM StateTable WHERE CountryID = '<CountryID>') OR
CityID IN (SELECT * FROM StateTable WHERE StateID IN (SELECT StateID FROM StateTable WHERE CountryID = '<CountryID>')))
这也可以使用 JOIN 来完成。