【问题标题】:SQL query with multiple table joins that interjoins具有相互连接的多个表连接的 SQL 查询
【发布时间】:2013-11-26 11:11:19
【问题描述】:

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

我在网络编程方面有点尘土飞扬,只是重新开始了它,出于某种原因,我似乎总是做最好的噩梦......

反正我的问题是这样的:

我有 7 张桌子需要连接在一起,但不是都在同一张桌子上。 它们看起来像这样:


caravan_rarity

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


大篷车尺寸

id---tier---size---stop_points---guard----diversity---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


城市关系

id---tier---relation---trade_multiplier


城市大小

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 行的返回。

请指教

提前致谢

【问题讨论】:

  • 把内连接改成左连接怎么样?
  • 尝试在city_other_know加入条件中将And替换为OR

标签: mysql sql join foreign-keys


【解决方案1】:

在左侧找到可能匹配右侧的表格并使用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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2012-06-05
    • 2019-01-15
    • 1970-01-01
    • 2018-05-30
    • 2018-05-01
    相关资源
    最近更新 更多