你好,我叫MySQL查詢選擇控制檯(或在phpMyAdmin SQL CMD)如何在mySQL查詢中爲每個結果插入記錄?
SELECT HF, ID FROM AH WHERE HF !=0 GROUP BY HF;
這一點,我需要從之前的每個返回行中執行命令後選擇
foreach
IF (SELECT COUNT(*) FROM `TOT` WHERE `ID` = @selectedID AND `Uto` = @selectedHF) = 0 THEN
INSERT INTO `TOT` (`ID`, `HF`) VALUES (@selectedID, @selectedHF);
END IF;
endforeach;
我該怎麼做「foreach」循環在mysql中?
[MySQL Cursors](http://www.mysqltutorial.org/mysql-cursor/)應該是你正在尋找的。 – SaschaM78 2015-04-01 15:19:47
遊標會完成任務,但是[INSERT INTO ..... SELECT](http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html)的樣子就像它也可能完成目標一樣。遊標一般不好用,他們可以避免的地方 – Ghost 2015-04-01 15:34:55