| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > 数据库开发

实例讲解"MySQL"在记录不存在时的插入

时间:2008-03-17 12:24:17  来源:Linux联盟收集整理  作者:
 

MySQL在记录不存在时的插入: MZDLinux联盟

示例:插入多条记录 MZDLinux联盟

假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: MZDLinux联盟

INSERT INTO clients MZDLinux联盟

(client_id, client_name, client_type) MZDLinux联盟

SELECT supplier_id, supplier_name, 'advertising' MZDLinux联盟

FROM suppliers MZDLinux联盟

WHERE not exists (select * from clients MZDLinux联盟

where clients.client_id = suppliers.supplier_id); MZDLinux联盟

示例:插入单条记录 MZDLinux联盟

INSERT INTO clients MZDLinux联盟

(client_id, client_name, client_type) MZDLinux联盟

SELECT 10345, 'IBM', 'advertising' MZDLinux联盟

FROM dual MZDLinux联盟

WHERE not exists (select * from clients MZDLinux联盟

where clients.client_id = 10345); MZDLinux联盟

使用 dual 做表名可以让你在 select 语句后面直接跟上要插入字段的值,即使这些值还不存在当前表中。MZDLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门