16

Is it possible ?

INSERT INTO tblPeople (id, group, company) VALUES (1, 'myGroup', 
IF($company = '') BEGIN 'no company' ELSE 'myCompany' END)  

I would like test a value, an if the variable $company is empty, I would like write no company.

Robert
  • 25,425
  • 8
  • 67
  • 81
Whitney R.
  • 630
  • 4
  • 10
  • 20

2 Answers2

28

Try this:

INSERT INTO tblPeople (id, group, company) 
select 1, 'myGroup', 
case 
  when @company is null or  @company = '' then 'no company' 
  else 'myCompany' 
END as  company
/*from tab --<-- optional*/
Robert
  • 25,425
  • 8
  • 67
  • 81
  • not working for me ? is this depends on mysql version ? I have created a table like this "create table tutorials_tbl( id INT NOT NULL , company VARCHAR(500) NOT NULL, PRIMARY KEY ( id ) ); " – Mugeesh Husain Jan 11 '21 at 08:51
6

Probably not, but

INSERT INTO tblPeople (id, [group], company) 
Select 1, 'myGroup', Case When @Company = '' then 'no company' Else 'my company' End

Should be a goer

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39