【问题标题】:php join 3 tables then insert to 4thphp加入3个表然后插入到第4个
【发布时间】:2016-02-01 16:32:32
【问题描述】:

这是 JOIN:

SELECT 
    gc3025_raid_results.raid_id, 
    gc3025_game_units.planet_id, 
    gc3025_game_units.g_dist, 
    gc3025_game_units.unit_name, 
    gc3025_units_base.unit_type, 
    gc3025_game_units.code, 
    gc3025_game_units.cdamage, 
    gc3025_units_base.defense_raid_score, 
    gc3025_units_base.raid_damage 
FROM gc3025_game_units 
    JOIN gc3025_units_base 
        ON gc3025_game_units.code = gc3025_units_base.unit_code 
    JOIN gc3025_raid_results 
        ON gc3025_raid_results.district_idg = gc3025_game_units.g_dist

然后我需要插入到另一个表中

INSERT INTO gc3025_raid_defenders( 
    raid_ids, planet_id, district_id, 
    unit_name, unit_type, unit_code, 
    raid_score, raid_damage)
    SELECT ... (see above) ...

任何建议将不胜感激。

【问题讨论】:

标签: php mysql sql-insert jointable


【解决方案1】:

这应该可以帮助您,但您需要添加其他字段。确保 insert() 中字段的名称和顺序与 select() 中的字段匹配

insert into gc3025_raid_defenders (raid_ids, planet_id, etc...)
select (gc3025_raid_results.raid_id, gc3025_game_units.planet_id, etc...)
from gc3025_game_units gu
join gc3025_units_base ub
    on gu.code = ub.unit_code
join gc3025_raid_results rr
    on gu.g_dist = rr. district_idg

顺便说一句,如果可能,请考虑在表之间一致地命名您的字段,例如 g_dist。

【讨论】:

  • 谢谢。我正在这样做,但是当我加入两个表时,有时这些字段名称会搞砸。它几乎奏效了。对于 Select 括号中的每一列,我在字段列表中都有未知列。然后我给它们添加了单引号,现在它说“操作数应该包含 1 列。
  • 哎呀。我声明了 gu、ub 和 rr 的表别名,所以你的字段名应该是 rr.raid_id、gu.planet_id 等...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
相关资源
最近更新 更多