插入表中从另一个获取信息

时间:2014-10-13 04:53:48

标签: mysql sql insert-select

db中的

我有2个表“npc”和“droplist”

CREATE TABLE `npc`(  
  `id` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `idTemplate` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `name` varchar(200) NOT NULL DEFAULT '',  
  `serverSideName` tinyint(1) NOT NULL DEFAULT '0',  
  `title` varchar(45) NOT NULL DEFAULT '',  
  `serverSideTitle` tinyint(1) NOT NULL DEFAULT '0',  
  `class` varchar(200) DEFAULT NULL,  
  `collision_radius` decimal(6,2) DEFAULT NULL,  
  `collision_height` decimal(6,2) DEFAULT NULL,  
  `level` tinyint(2) DEFAULT NULL,  
  `sex` enum('etc','female','male') NOT NULL DEFAULT 'etc',  
  `type` varchar(22) DEFAULT NULL,  
  `attackrange` smallint(4) DEFAULT NULL,  
  `hp` decimal(30,15) DEFAULT NULL,  
  `mp` decimal(30,15) DEFAULT NULL,  
  `hpreg` decimal(30,15) DEFAULT NULL,  
  `mpreg` decimal(30,15) DEFAULT NULL,  
  `str` tinyint(2) NOT NULL DEFAULT '40',  
  `con` tinyint(2) NOT NULL DEFAULT '43',  
  `dex` tinyint(2) NOT NULL DEFAULT '30',  
  `int` tinyint(2) NOT NULL DEFAULT '21',  
  `wit` tinyint(2) NOT NULL DEFAULT '20',  
  `men` tinyint(2) NOT NULL DEFAULT '20',  
  `exp` int(9) NOT NULL DEFAULT '0',  
  `sp` int(9) NOT NULL DEFAULT '0',  
  `patk` decimal(12,5) DEFAULT NULL,  
  `pdef` decimal(12,5) DEFAULT NULL,  
  `matk` decimal(12,5) DEFAULT NULL,  
  `mdef` decimal(12,5) DEFAULT NULL,  
  `atkspd` smallint(4) NOT NULL DEFAULT '230',  
  `critical` tinyint(1) NOT NULL DEFAULT '1',  
  `aggro` smallint(4) NOT NULL DEFAULT '0',  
  `matkspd` smallint(4) NOT NULL DEFAULT '333',  
  `rhand` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `lhand` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `enchant` tinyint(1) NOT NULL DEFAULT '0',  
  `walkspd` decimal(10,5) NOT NULL DEFAULT '60',  
  `runspd` decimal(10,5) NOT NULL DEFAULT '120',  
  `targetable` tinyint(1) NOT NULL DEFAULT '1',  
  `show_name` tinyint(1) NOT NULL DEFAULT '1',  
  `dropHerbGroup` tinyint(1) NOT NULL DEFAULT '0',  
  `basestats` tinyint(1) NOT NULL DEFAULT '0',  
  PRIMARY KEY (`id`,`idTemplate`)  
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `droplist` (  
  `mobId` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `itemId` smallint(5) unsigned NOT NULL DEFAULT '0',  
  `min` int(8) unsigned NOT NULL DEFAULT '0',  
  `max` int(8) unsigned NOT NULL DEFAULT '0',  
  `category` smallint(3) NOT NULL DEFAULT '0',  
  `chance` mediumint(7) unsigned NOT NULL DEFAULT '0',  
  PRIMARY KEY (`mobId`,`itemId`,`category`),  
  KEY `key_mobId` (`mobId`)  
) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

如何插入'droplist' - > 'mobid''itemid''min''max''类别''机会',其中“droplist.mobid = npc.id”,来自npc.type = L2Raidboss和npc.level> = 76;

INSERT('''6673''10''15''1''1000000')INTO droplist WHERE mobid IN(SELECT id FROM npc WHERE type ='L2RaidBoss'AND level> ='76');

肯定这不行,但需要这样的东西

2 个答案:

答案 0 :(得分:0)

使用select查询查看npc表中是否存在mobid以及(type = L2Raidboss和level> = 76)等要求

如果select查询返回结果,请继续执行droplist表的插入过程

加入:

如果每行的值相同/不变,

执行第一个查询:

SELECT id FROM npc WHERE type = 'L2RaidBoss' AND level >= '76'

将结果存储在数组中,继续第二个查询

while($array_results[id] ....)//depending on language that you will be using
{
insert into droplist(mobid,itemid,min,max,category,chance) values ('$array_results[id]','6673','10','15','1','1000000')
}

这取决于您将使用的语言,您将使用php吗?

答案 1 :(得分:0)

试试这个:

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

或者如果您想从另一个数据库加载,您可以编写如下数据库迁移脚本:这将提供MySql Officail site方面从cst文本文件导入。 供参考

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]