当一张表的多个字段没有唯一索引时,想要在sql语句中加上判断达到不插入重复记录的目的,可以使用mysql的dual
INSERT INTO book_chapter_del
(book_id, chapter_id,status)
SELECT 20049198, 1000930, 0
FROM dual
WHERE not exists (select * from book_chapter_del
where book_id = 20049198 and chapter_id=1000930) limit 1;
(book_id, chapter_id,status)
SELECT 20049198, 1000930, 0
FROM dual
WHERE not exists (select * from book_chapter_del
where book_id = 20049198 and chapter_id=1000930) limit 1;
select express [from dual]; mysql总是作为返回该表达式值的普通select语句执行,返回一行记录的结果集,from dual 对mysql来说根本就是摆设!而oracle里该句必须有from dual;否则报错!
select express from dual where 0=2; mysq 和 oracle的行为一致:该句就如同你认为的正常表那样——会先计算where的条件,再行计算express;这里的where条件会决定expres是否会返回
本文转自 dogegg250 51CTO博客,原文链接:http://blog.51cto.com/jianshusoft/677868,如需转载请自行联系原作者