-1

我有 4 张桌子,promoSchedule

id | startdate | enddate  
1 |1 july | 10 july  
2 |1 july | 20 july  
3 |2 july | 30 july  
4 |1 july |15 aug  

还有 3 个表,promoScheduleCountry、promoScheduleState 和 promoScheduleCity。这些表中的数据是,

promoScheduleCountry  
id | promoScheduleId | countryId  
1  | 1               | US

promoScheduleStates  

id | promoScheduleId | stateId  
1  | 2               | New york 



promoScheduleCities  
id | promoScheduleId | cityId  
1  | 3               | Amsterdam  
2  | 4               | Geneva   

国家,州和城市是releted。

现在想要所有promoScheduleIds,如果提供countryId 说US,那么从3 个表中,即promoScheduleCountry、promoScheduleState 和promoScheduleCity,我想要promoScheduleIds 1、2、3、4。

如果我promovide stateId 说纽约,那么从2 个表中,即promoScheduleState 和promoScheudleCity,我想要promoScheduleIds 2、3、4。

我想要这个查询或存储过程或指导如何做到这一点?
为了便于理解,我有国家、州和城市的名称。实际上还有 3 个国家、州和城市的表格。并且它们彼此相关,即在状态表中有国家的主键,在城市表中有状态表的主键

4

2 回答 2

0

你应该像下面这样重新设计你的桌子

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 来完成。

于 2013-07-26T06:44:59.587 回答
0

这不是对您问题的直接回答,而是更多关于表设计的查询,这似乎有点不必要地复杂(正如您在查询时发现的那样)。

您可以用一个 PromoLocations 表替换您的三个 promoXXX 表:

ID    PromoScheduleID   CountryID   StateID   CityID

这可能会使您的代码更容易编码和查询。

于 2013-07-26T05:48:54.503 回答