简体   繁体   English

在MySql中使用存储过程更新2个表

[英]Update 2 tables using Stored Procedure in MySql

This seems like a simple request. 这似乎是一个简单的请求。 But my query in not working and I'm finding conflicting answers on the internet. 但是我的查询无法正常工作,并且我在互联网上发现矛盾的答案。 Is it possible to have UPDATE and INSERT using a Stored Procedure joining 2 tables in MySql? 是否可以使用存储过程连接MySql中的2个表来进行UPDATE和INSERT?

I have an Asp.net Webforms website. 我有一个Asp.net Webforms网站。 It has 2 tables Individual and Address. 它具有2个表“个人”和“地址”。 Individual table contains data on an individual, ie Phone Number, Fax, Email ect. 个体表包含有关个体的数据,即电话号码,传真,电子邮件等。 The address table has all the address information for an individual. 地址表包含个人的所有地址信息。 They each table has a column of Individual ID which auto increments. 它们每个表都有一列“个人ID”,该列会自动递增。 (Note: the individualID in Address table is not a primary key, but individualID in the individual table is a primary key. (注意:地址表中的personalID不是主键,但个人表中的personalID是主键。

Anyway, I have a FormView in Asp.net that with a SELECT statement that joins those 2 tables and display the data fine. 无论如何,我在Asp.net中有一个FormView,其中的SELECT语句连接了这两个表并显示了数据。 But updating new information to both tables keeps failing. 但是,将新信息更新到两个表中始终会失败。

My most recent error is : Duplicate entry '0' for key 'PRIMARY' 我最近的错误是:键“ PRIMARY”的条目“ 0”重复

Is there a way to write an UPDATE statement that joins 2 Tables?? 有没有一种方法可以编写连接2个表的UPDATE语句? This has to exist right? 这必须存在吗?

It is possible to update multiple tables with a single query - 可以通过单个查询更新多个表-

UPDATE table1
INNER JOIN table2
  ON table1.id = table2.table1_id
SET table1.col1 = 'some value', table2.col1 = 'Another value'
WHERE <some where clause>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM