从国家/地区和州名表中获取数据

时间:2013-07-26 05:07:36

标签: mysql sql

我有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   

国家,州和城市相互关联。

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

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

我想要这个或指南的查询或存储过程如何做到这一点?
为了理解目的,我没有国家,州和城市的名字。实际上还有3个表国家,州和城市。并且它们与状态表中的每个相关联有国家的主键,在城市表中有状态表的主键

2 个答案:

答案 0 :(得分:0)

这不是你问题的直接答案,而是更多关于你的表设计的查询,这看起来有点不必要的复杂(正如你在查询时发现的那样)。

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

ID    PromoScheduleID   CountryID   StateID   CityID

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

答案 1 :(得分: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来完成。