第1个回答 推荐于2017-11-26
一条语句实现的方法不如存储过程方便,而且不如存储过程效率高,楼主写个存储过程会比较好
INSERT INTO log (date, ip) SELECT '2014-01-01' as date, '192.168.1.1' as ip FROM log WHERE NOT EXISTS ( SELECT * FROM log t WHERE t.data = '2014-01-01' AND t.ip = '192.168.1.1')本回答被提问者采纳
第2个回答 2014-01-17
-- 我这里假设你那个 id 是 自动递增的主键.
INSERT INTO log (date, ip)
SELECT '2014-01-01', '192.168.1.1'
FROM dual
WHERE NOT EXISTS (
SELECT *
FROM log t
WHERE t.data = '2014-01-01' AND t.ip = '192.168.1.1'
)
第3个回答 2014-01-17
mysql 有判断语法
有四种方式进行判断:1. SHOW TABLES LIKE '%tb_bp_d_case%';2. select TABLE_NAME from INFORMATION_SCHEMA.TABLES whereTABLE_SCHEMA='dbname' and TABLE_NAME='tablename' ;3. 如果表不存在就建立这个表,那么可以直接用 create table if not exists tablename 这样的指令来建立,不需要先去查询表是否存在。4. 从模板表创建表:create table if not exists like old_table_name;
自己去套
第4个回答 2014-01-17
INSERT INTO log (date, ip) SELECT '2014-01-01' as date, '192.168.1.1' as ip FROM log WHERE NOT EXISTS (
SELECT *
FROM log t
WHERE t.data = '2014-01-01' AND t.ip = '192.168.1.1'
)