具有多个表连接的SQL查询,它们相互连接

时间:2013-11-26 11:11:19

标签: mysql sql join foreign-keys

我是这个地方的新手,虽然我认为我以前使用过这里的解决方案。

我在网络编程方面有点尘土飞扬,只是再次把它拿起来,出于某种原因,我似乎总是得到最好的噩梦......

无论如何我的问题是:

我有7个表需要连接在一起,但并非所有表都在同一个表中。 他们看起来像这样:


caravan_rarity

ID --- ---层类型--- --- chance_interval_start --- chance_interval_end trade_multiplier


caravan_size

ID --- ---层尺寸--- --- stop_points后卫---- ---多样性--- price_first --- price_increase trade_multiplier


caravan_stop

ID --- --- fk_stop_one --- fk_stop_two fk_stop_three


city_other_known

ID --- ---名--- fk_city_size_id --- fk_city_relation_id距离--- upgrade_relation


city_relation

ID --- ---层关系--- trade_multiplier


city_size

ID --- ---层尺寸--- --- lost_revenue --- base_trade_value --- population_range_min --- population_range_max trade_multiplier


fogcity_caravan

ID --- --- fk_caravan_size_id --- fk_city_start --- fk_caravan_stop_id --- fk_city_end fk_caravan_rarity_id


列名称是fk的任何地方,这是一个链接到具有相同名称而没有fk的列的外键(例如:fk_caravan_size_id链接到caravan_size.id)

例外是在caravan_stop表中。所有fk列都链接到city_other_known.id。 同样在fogcity_caravan中,fk_city_start和fk_city_end都链接到city_other_known.id

另一个注意事项:唯一允许为NULL的comlumns是caravan_stop.fk_stop_one,caravan_stop.fk_stop_two和caravan_stop.fk_stop_three

所以我们尝试查询所有这些:

SELECT 
    caravan_size.size, 
    caravan_size.diversity, 
    caravan_size.trade_multiplier, 
    city_other_known.name, 
    city_size.trade_multiplier, 
    city_relation.relation, 
    city_relation.trade_multiplier, 
    caravan_rarity.type, 
    caravan_rarity.trade_multiplier
FROM fogcity_caravan 
INNER JOIN caravan_size ON fogcity_caravan.fk_caravan_size_id = caravan_size.id 
INNER JOIN caravan_stop ON fogcity_caravan.fk_caravan_stop_id = caravan_stop.id 
INNER JOIN city_other_known ON fogcity_caravan.fk_city_start = city_other_known.id 
       AND fogcity_caravan.fk_city_end = city_other_known.id 
       AND caravan_stop.fk_stop_one = city_other_known.id 
       AND caravan_stop.fk_stop_two = city_other_known.id 
       AND caravan_stop.fk_stop_three = city_other_known.id 
INNER JOIN caravan_rarity ON fogcity_caravan.fk_caravan_rarity_id = caravan_rarity.id 
INNER JOIN city_size ON city_other_known.fk_city_size_id = city_size.id 
INNER JOIN city_relation ON city_other_known.fk_city_relation_id = city_relation.id

而且......正如你们中的一些人可能已经看到的那样(我没有线索天气或者没有这是显而易见的)我得到了0行的回报。

请咨询

提前致谢

1 个答案:

答案 0 :(得分:0)

找到左侧的表格,右侧可能存在匹配并使用LEFT JOIN。对于那些将使用INNER JOIN明确匹配的人。

请参阅链接here.

例如,你可能在这里LEFT JOIN:

 LEFT JOIN city_other_known ON fogcity_caravan.fk_city_start = city_other_known.id 
   AND fogcity_caravan.fk_city_end = city_other_known.id 
   AND caravan_stop.fk_stop_one = city_other_known.id 
   AND caravan_stop.fk_stop_two = city_other_known.id 
   AND caravan_stop.fk_stop_three = city_other_known.id 

使用OR代替AND:

LEFT JOIN city_other_known ON fogcity_caravan.fk_city_start = city_other_known.id 
   OR fogcity_caravan.fk_city_end = city_other_known.id 
   OR caravan_stop.fk_stop_one = city_other_known.id 
   OR caravan_stop.fk_stop_two = city_other_known.id 
   OR caravan_stop.fk_stop_three = city_other_known.id 
相关问题