连续更新SQL表

时间:2016-10-01 15:20:16

标签: php mysql database

我有一个脚本将新用户插入表中,用户由id定义。我在其他几个表中使用id来将某些方面与该用户相关联。例如:

zone.js:355 Unhandled Promise rejection: No provider for ElementRef! ; Zone: <root> ; Task: Promise.then ; Value: NoProviderError {_nativeError: Error: No provider for ElementRef!
    at NoProviderError.Error (native)
    at NoProviderError.Base…, keys: Array[1], injectors: Array[1]}_nativeError: Error: No provider for ElementRef!
    at NoProviderError.Error (native)
    at NoProviderError.BaseError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1248:38)
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1703:20)
    at new NoProviderError (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1734:20)
    at ReflectiveInjector_._throwOrNull (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3331:23)
    at ReflectiveInjector_._getByKeyDefault (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3359:29)
    at ReflectiveInjector_._getByKey (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3322:29)
    at ReflectiveInjector_.get (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3131:25)
    at NgModuleInjector.AppModuleInjector.createInternal (AppModule.ngfactory.js:310:75)
    at NgModuleInjector.create (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:7192:80)constructResolvingMessage: (keys)injectors: Array[1]keys: Array[1]message: (...)name: (...)stack: (...)__proto__: AbstractProviderError Error: No provider for ElementRef!
    at NoProviderError.Error (native)
    at NoProviderError.BaseError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1248:38)
    at NoProviderError.AbstractProviderError [as constructor] (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1703:20)
    at new NoProviderError (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:1734:20)
    at ReflectiveInjector_._throwOrNull (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3331:23)
    at ReflectiveInjector_._getByKeyDefault (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3359:29)
    at ReflectiveInjector_._getByKey (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3322:29)
    at ReflectiveInjector_.get (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:3131:25)
    at NgModuleInjector.AppModuleInjector.createInternal (AppModule.ngfactory.js:310:75)
    at NgModuleInjector.create (http://localhost:56159/node_modules/@angular/core//bundles/core.umd.js:7192:80)consoleError @ zone.js:355_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308
zone.js:357 Error: Uncaught (in promise): Error: No provider for ElementRef!(…)consoleError @ zone.js:357_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308

在上面的三个表中,我使用USER TABLE中的id将该条目与另外两个表中的userID相关联。

我试图从USER TABLE获取最新的id(max(id))以将默认值输入到其他两个表中。例如,当用户3(id = 3,auto_increment)通过我的主页上的入职流程添加到USER TABLE时,SQL脚本将获取id号并将其输入到其他两个表中的userID以及默认值cat1,cat2和track1的值。有一种有效的方法可以使用一个查询或一个可以一次发送的查询块来执行此操作吗?

2 个答案:

答案 0 :(得分:1)

使用last_insert_id()

insert into usertable ( phone ) values ( '12345' );

set @userid = last_insert_id();

insert into category ( userid, cat1, cat2 )
    values (@userid, 'cat1', 'cat2');

insert into tracking ( userid, track1 )
    values (@userid, 'track1');

答案 1 :(得分:0)

window_width()/2

您应该在usertable的phone字段上放置一个唯一约束,并在事务中包装这些查询,这样就没有重复。