问题描述
我正在Java中使用MySQL.
Im working with MySQL in java.
我正在尝试更新表'regApartmentsTable','gardApartmentsTable','penthousesTable'中的一个表中的'owners'字段,该字段为空,对应于特定的apartmentNum和street,并用字符串' newOwners'.
I'm trying to update the 'owners' field in one of the tables 'regApartmentsTable', 'gardApartmentsTable', 'penthousesTable', which is empty, and corresponds to specific apartmentNum and street, and replace it with the string 'newOwners'.
为此,我编写了以下代码:
In order to do that I've wrote the following code:
st=connection.prepareStatement("UPDATE regApartmentsTable,gardApartmentsTable,penthousesTable SET owners=? " +
"WHERE owners=? AND apartmentNum=? AND street=?");
st.setString(1, newOwners);
st.setString(2, "");
st.setInt(3, apartmentNum);
st.setString(4, streetName+" "+buildingNum);
我包括3个表,因为我需要查看所有表. (所需的没有主人的公寓,并且与公寓的编号和街道相匹配,如果可以帮助任何人,则它不能在一个以上的桌子中).
I include the 3 tables since I need to look in all of them. (The required apartment, which has no owners, and matches the apartmentNum and street, cannot be in more than one table, if it helps anyone).
但是,当我尝试运行此代码时,出现字段中的列所有者"不明确"错误.有什么想法我还应该写SQL命令吗?谢谢你!
But, when I try to run this code, I get a "Column 'owners' in field is ambiguous" error.Any ideas how else should I write the SQL command ?thanks ahead!
我没有对我的问题给出足够的答案...好的,我知道会引发异常,因为这三个表中的所有者"字段很常见.但是,我该如何解决问题呢?我无法在表名前添加前缀,因为我不知道要在哪个表中找到所需的公寓...如果知道,我将不会在3个表中进行搜索.
I didn't get a sufficient answer to my problem... Ok, I understood that the exception raises since 'owners' field is common in those three tables.And yet, how do I solve the problem? I cannot add a prefix with the table's name since I do not know in which table I'm going to find the required apartment... If I knew so, I wouldn't have searched in 3 tables.
推荐答案
MySQL中的多个表UPDATE只是表联接的一种形式,使用regApartmentsTable.owners
等.
The multiple tables UPDATE in MySQL is just a form of table join, using regApartmentsTable.owners
and such.
这里的每个表都需要一个单独的UPDATE,因为联接不是您打算进行的更新.或制作一个基本表.
You need a separate UPDATE for every table here, as a join is not what you intend for the update. Or make a base table.
str = connection.prepareStatement("UPDATE regApartmentsTable SET owners=? " +
"WHERE owners=? AND apartmentNum=? AND street=?");
str.setString(1, newOwners);
str.setString(2, "");
str.setInt(3, apartmentNum);
str.setString(4, streetName+" "+buildingNum);
str = connection.prepareStatement("UPDATE gardApartmentsTable SET owners=? " +
"WHERE owners=? AND apartmentNum=? AND street=?");
stg.setString(1, newOwners);
stg.setString(2, "");
stg.setInt(3, apartmentNum);
stg.setString(4, streetName+" "+buildingNum);
stp = connection.prepareStatement("UPDATE penthousesTable SET owners=? " +
"WHERE owners=? AND apartmentNum=? AND street=?");
stp.setString(1, newOwners);
stp.setString(2, "");
stp.setInt(3, apartmentNum);
stp.setString(4, streetName+" "+buildingNum);
这篇关于字段中的列''不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!