如何将两个不同的数组插入到表中的SQL INSERT INTO语句?
P粉959676410
P粉959676410 2024-02-17 18:59:52
0
1
366

自从我搞乱 SQL 以来已经很长时间了,当我构建自己的视频游戏时,我不得不重新开始使用它。我正在使用 MySQL,并且在这里遇到了一些复杂的问题。

这是我的两个数组;

SET @myArrayofRaces = '"Dwarf", "Elf", "Halfling", "Human", "Dragonborn", "Gnome", "Half-Elf", "Half-Orc", "Tiefling"';
SET @myArrayofClasses = '"Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Warlock", "Wizard"';

正如我们所看到的,我有 9 场比赛和 12 个班级。我想要使​​用这些数组编写 INSERT INTO 语句的代码,这样我就不必手动输入 108 行。

这是我正在运行的 INSERT INTO 语句;

INSERT INTO world_content.character_create_template (
        race,
        aspect,
        instance,
        pos_x,
        pos_y,
        pos_z,
        orientation,
        faction,
        autoAttack,
        race_icon,
        class_icon,
        race_description,
        class_description,
        isactive,
        respawnInstance,
        respawnPosX,
        respawnPosY,
        respawnPosZ,
        startingLevel,
        sprint 
    )
VALUES
    (
        437,
        428,
        29,
        - 170,
        74,
        154,
        0,
        1,
        - 1,
        "Assets/Resources/Assets/Icons/Race Icons/Dwarf.png",
        "Assets/Resources/Assets/Icons/Class Icons/Druid.png",
        "Dwarf",
        "Druid",
        1,
        29,
        - 170,
        74,
        154,
        1,
        - 1 
    )

我需要循环这个 INSERT INTO 语句,直到我完成了所有 108 种种族和类别的组合。因此,矮人将作为每个类别出现在数据库中。然后 Elf 将被插入到数据库中的每个类。然后是半身人,然后是人类,等等。

类数组只是进入 class_description 和 class_icon 中,您可以看到我正在删除图像的链接。比赛也将遵循race_icon图像。

有谁知道如何在每次比赛中循环 ArrayofRaces 12 次,以便我可以轻松导入类别和比赛?

提前致谢!

P粉959676410
P粉959676410

全部回复(1)
P粉505917590

您可以使用交叉连接生成所有组合。例如:

INSERT INTO character_create_template (
        race,
        aspect,
        instance,
        pos_x,
        pos_y,
        pos_z,
        orientation,
        faction,
        autoAttack,
        race_icon,
        class_icon,
        race_description,
        class_description,
        isactive,
        respawnInstance,
        respawnPosX,
        respawnPosY,
        respawnPosZ,
        startingLevel,
        sprint 
    )
with
race as (
  select 'Dwarf' as name
  union all select 'Elf' -- repeat this line for more races
),
class as (
  select 'Barbarian' as name
  union all select 'Bard' -- repeat this line for more classes
)
select 
        437,
        428,
        29,
        - 170,
        74,
        154,
        0,
        1,
        - 1,
        "Assets/Resources/Assets/Icons/Race Icons/Dwarf.png",
        "Assets/Resources/Assets/Icons/Class Icons/Druid.png",
        r.name,
        c.name,
        1,
        29,
        - 170,
        74,
        154,
        1,
        - 1 
from race r
cross join class c

请参阅 DB Fiddle 处的运行示例。

注意:此示例包括两项比赛和两个级别,总共产生 4 种组合。添加其余部分,查询将生成全部 108 个。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板