MySQL:从SELECT语句到INSERT语句

时间:2013-11-05 02:52:44

标签: mysql sql sql-insert

我有以下代码返回两个很好的表(在PhpMyAdmin中使用SQL函数时)。但是,我无法将其插入TABLE B

如何在表B中插入此内容而不是仅显示它?

SELECT DateTimeCode, Rat,
MAX(IntendedStimulusDuration_ms) AS StimulusDuration,
SUM(Correct + Incorrect + Omission + PrematureNosepokes) AS total_trials,
SUM(Correct) AS correct,
SUM(Incorrect) AS incorrect,
SUM(Omission) AS omission,
SUM(PrematureNosepokes) AS premature,
SUM(PerseverativePanelPushes) AS P_PanelPushes,
SUM(PerseverativeNosepokes) AS P_nosepokes,
SUM(PerseverativeNosepokesSameHole) AS P_NPsame,
SUM(PerseverativeNOsepokesOtherHoles) AS P_NPother
FROM `FiveChoice_TrialData`
GROUP BY Rat,DateTimeCode;

--If correct = 1
SELECT DateTimeCode, Rat,
AVG(ResponseLatency_ms) AS ResponseLatency,
AVG(CollectionLatency_ms) AS CollectionLatency
FROM `FiveChoice_TrialData`
WHERE Correct = 1
GROUP BY Rat,DateTimeCode;

基本上我试过了:

INSERT INTO TABLE_B (--all my col names, just like the alias stated above)
VALUE (--My two select statement as written above, separated by a coma)

1 个答案:

答案 0 :(得分:4)

从查询插入时,您不需要value语句。试试这个:

insert into table_b(<list of columns here>)
    SELECT DateTimeCode, Rat,
    MAX(IntendedStimulusDuration_ms) AS StimulusDuration,
    SUM(Correct + Incorrect + Omission + PrematureNosepokes) AS total_trials,
    SUM(Correct) AS correct,
    SUM(Incorrect) AS incorrect,
    SUM(Omission) AS omission,
    SUM(PrematureNosepokes) AS premature,
    SUM(PerseverativePanelPushes) AS P_PanelPushes,
    SUM(PerseverativeNosepokes) AS P_nosepokes,
    SUM(PerseverativeNosepokesSameHole) AS P_NPsame,
    SUM(PerseverativeNOsepokesOtherHoles) AS P_NPother
    FROM `FiveChoice_TrialData`
    GROUP BY Rat,DateTimeCode;

第二个查询的类似内容。