mysqlinsert-into

Can I check condition, then insert value to table in SQL


I am beginner. So I don't know something. I am trying insert value. But i can't. So i need your help.

My code:

INSERT INTO company_user (stamp_img) values ('test.png') SELECT * FROM company_user WHERE company_register = '123456789';

But Select,

syntax error: select is not valid input at this position.

How to check condition. I want to check register is true then insert value.

So when i write

`INSERT INTO company_user (stamp_img) values ('test.png')`

Error Code: 1062. Duplicate entry '' for key 'company_register_UNIQUE'

What should I do? Table Structure

1: enter image description here


Solution

  • You are probably looking for updating the table. So you should use the UPDATE statement:

    UPDATE company_user SET stamp_img = 'test.png' WHERE company_register = '123456789';
    

    This will modifiy the row in your table where company_register is 123456789 setting the stamp_img to test.png.

    Is this what you are looking for?