Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL : a condition (IF-ELSE) in INSERT INTO

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.

like image 674
Whitney R. Avatar asked Jan 08 '13 12:01

Whitney R.


1 Answers

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*/
like image 190
Robert Avatar answered Oct 02 '22 13:10

Robert