第一次用MySql,写出如下语句:
INSERT INTO tb (id,TName) VALUES(1, 'a new name') WHERE not exists (SELECT TName FROM tb WHERE TName='a old name');
报错,百思不得其解。google之后的答案:
INSERT INTO tb (id,TName) SELECT 1, 'a new name' FROM dual WHERE not exists (SELECT TName FROM tb WHERE TName='a old name');
关于DUAL表:
DUAL is purely for the convenience of people who require that all SELECT statements should have FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no tables are referenced.
MySql5.5指南:http://dev.mysql.com/doc/refman/5.5/en/select.html