Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从国家和州表中获取数据_Mysql_Sql - Fatal编程技术网

Mysql 从国家和州表中获取数据

Mysql 从国家和州表中获取数据,mysql,sql,Mysql,Sql,我有四张桌子 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

我有四张桌子

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   
国家、州和城市是关联的

现在我想要所有的PromoScheduleID,如果我提供countryId,比如我们,那么从3个表,即promoScheduleCountry,promoScheduleState和promoScheduleCity,我想要PromoScheduleID 1,2,3,4

如果我提供stateId,比如说纽约,那么从两个表,即promoScheduleState和promoScheduleCity,我需要promoScheduleID 2,3,4

我想要这个指南的查询或存储过程,或者如何做?
为了便于理解,我没有国家、州和城市的名称。事实上,还有3张表格,分别是国家、州和城市。这不是对你问题的直接回答,更多的是对你的表的设计的查询,这似乎有点不必要的复杂,因为当你去查询它时,你会发现它

您可以将三个promoXXX表替换为一个PromoLocations表:

ID    PromoScheduleID   CountryID   StateID   CityID

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

您应该像下面这样重新设计您的表

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、PromoScheduleState和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>')))
您的查询如下

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实现。

promoScheduleCountry、promoScheduleState和promoScheduleCity之间的关系是什么?这些表之间必须有某种关系,或者必须有另一个表来定义哪个城市属于哪个州,哪个州属于哪个国家。糟糕的数据库设计。如果这三个表之间存在关系,则您可以获得查询或存储过程的正确输出。@ITppl城市、州和国家表之间存在关系。实际上还有3个表country、state和city,promoScheduleCountry、promoSState和promoSCity表中使用了这些ID。@Vishal重新设计您的表Mate实际上要在promoLocation表中插入所有数据,维护起来会非常困难。country、state和city之间的关系与我的表中的关系完全相同,其ID用于promoScheduleCountry、promoScheduleState和promoScheduleCity表。如果我必须使用promoScheduleCountry、promoScheduleState和promoScheduleCity表,那么将如何查询,而不是使用临时表,从所有三个表中获取promoScheduleId并将其存储在临时表中,然后使用临时表的promoScheduleId获取结果