Home > Database > Mysql Tutorial > body text

mysql有条件插入数据_MySQL

WBOY
Release: 2016-06-01 13:43:38
Original
1266 people have browsed it

bitsCN.com 要求是这样的: 我有一个表存着基础数据有一个字段是 管理号 我的目的是先查询最新的管理号按照规则生成新管理号 然后insert到表里新数据  由于查询并不锁表 所以在查询到insert这步中间会有可能两线程查询到相同的管理号?  关键是如下的MYSQL语句, 插入多条记录: [sql]   INSERT INTO clients   (client_id, client_name, client_type)   SELECT supplier_id, supplier_name, 'advertising'   FROM suppliers   WHERE not exists (select * from clients   where clients.client_id = suppliers.supplier_id);    插入单条记录: [sql]   INSERT INTO clients   (client_id, client_name, client_type)   SELECT 10345, 'IBM', 'advertising'   FROM dual   WHERE not exists (select * from clients   where clients.client_id = 10345);    所以在插入时可以检测管理号存在不存在,插入若不成功则取出目前的最新管理号,重新计算后再插入,直至成功。  这种情况很多的,比如火车、影院售票等,由于并发处理量很大,往往查询到有票的,但实际下单时此票已售出,不知道这些系统是如何处理的。   作者 meditatorzhang bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!