mysql - 从连接表INSERT INTO表,更新表有额外列

时间:2013-05-29 16:32:03

标签: mysql

我有一个具有这种结构的表:

CREATE TABLE `jos_cpnllist` (
    `MSTMLSNO` int(7),
    `CLTIME` TIMESTAMP NULL,
    PRIMARY KEY (`MSTMLSNO`))
    ENGINE=MyISAM 
    SELECT * FROM jos_mls AS mls
        INNER JOIN jos_activeagents AS active ON mls.MSTLISTBRD = active.AGENTUID
        INNER JOIN jos_agents AS agents ON active.AGENTUID = agents.AGTBRDIDMM;

然后我想定期更新此表中的数据:

INSERT INTO jos_cpnllist
    SELECT * FROM jos_mls AS mls
        INNER JOIN jos_activeagents AS active ON mls.MSTLISTBRD = active.AGENTUID
        INNER JOIN jos_agents AS agents ON active.AGENTUID = agents.AGTBRDIDMM;

添加时间戳列让我在进行更新时遇到麻烦:

ERROR 1136 (21S01): Column count doesn't match value count at row 1

有没有办法解决这个问题而没有专门列出SELECT中的列?告诉它忽略CLTIME时间戳列?

注意:这就像75列的数据,所以我想避免列出所有这些。 MSTMLSNO表中已存在jos_mls列,因此不会引发错误。 CLTIME是唯一一个在大型INNER JOIN语句中不存在的列。

1 个答案:

答案 0 :(得分:3)

使用insert时,始终显式指定列:

INSERT INTO jos_cpnllist(MSTMLSNO, CLTIME)
    SELECT <columnX> as MSTMLSNO, <columnY> as CLTIME
    FROM jos_mls AS mls
        INNER JOIN jos_activeagents AS active ON mls.MSTLISTBRD = active.AGENTUID
        INNER JOIN jos_agents AS agents ON active.AGENTUID = agents.AGTBRDIDMM;

列出75列并不难。您可以从INFORMATION_SCHEMA.columns获取列表,并在Excel中对其进行格式化。