Skip to content
Advertisement

Can I update a SQL table based on data from another table, with unknown data? [closed]

I’m making a NetBeans Servlet which accesses a MySQL database; and updates data based on HTML textfields. I need to update certain name and color in user table based if mail and pasword (which are in user_data) exist; but I do not know how.

In servlet

Statement st = (Statement) conexion.createStatement();
ps = conexion.prepareStatement("***Where I need the answer***")
ps.setString(1, name);
ps.setString(2, color);
ps.setString(3, mail);
ps.setString(4, password);
ps.executeUpdate();

In MySQL

create table if not exists user(
id_user int(6) not null primary key auto_increment,
mail nvarchar(40),
password nvarchar(20));

create table if not exists user_data(
id_user int(6),
name varchar(40),
color varchar(6),
grade float(2,1),
foreign key (id_user) references usuarios(id_user)
on delete cascade on update cascade);

Advertisement

Answer

I suspect that you are looking for:

update user_data ud
set name = ?, color = ?
where exists (
    select 1 
    from user u 
    where u.id_user = ud.id_user and u.email = ? and u.password = ?
)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement