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

我在网络编程方面有点尘土飞扬,只是又重新学习了它,由于某种原因,我似乎总是得到最好的噩梦...

无论如何,我的问题是这样的:

我有7个表需要联接在一起,但不是所有表都在同一表上。
他们看起来像这样:



caravan_rarity

id --- tier --- type --- chance_interval_start --- chance_interval_end --- trade_multiplier



caravan_size

id ---等级----大小---止损点----后卫-多样性---- price_first ----价格上涨---- 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 --- tier --- size --- 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的行是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行的返回。

请指教

提前致谢

最佳答案

在左侧找到可能匹配的表,然后使用LEFT JOIN。对于那些具有明确匹配项的对象,请使用INNER JOIN

见链接here.

例如,您可能在这里左加入:

 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

10-07 15:54