MySQL IF statement to check if table is empty -


how can create if statement insert default row if table found empty? i'm following logic wrong.

if ((select * mytable)=0) insert mytable (mycolumn) values (myvalue) end if;  #1064 - have error in sql syntax; check manual corresponds mysql server version right syntax use near 

answer comments (mcadam331) http://sqlfiddle.com/#!9/42229/1

create table mytable(   name varchar(100));  insert mytable (`name`) select 'namer' dual not exists (select * mytable); 

you can use this:

/* insert query */ insert mytable (     /* place values in 'from' part of query */     select tbl.*     ( select 1 id, 'username' username, 'password' password ) tbl     0 = (select count(*) mytable ) ); 

it starts insert part. after comes subquery select 'records' (which can give values in from ( select ... part. makes sure there inserts if no records exist.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -