insert row if table is empty, else update row (mysql) i have 2 tables "table1" and "table2" both have TIMESTAMP cols for starttime and endtime. i want something like 

if (table2 is empty)
insert into table2 (table2.starttime, table2.endtime) value (table1.starttime, table1.endtime (last row))
else if (TIMESTAMPDIFF(SECOND,table2.endtime(last row),table1.starttime(lastrow))&lt;10)
update table2.endtime (last row) value (table1.endtime)
else
insert into table2 (table2.starttime, table2.endtime) value (table1.starttime, table1.endtime (last row))


i have tried different aproches but i am unable to get it working specially IF-ELSE and IS NULL check.
i can get last row as (SELECT endtime from table1 order by endtime DESC limit 1)so kindly ignore that part.
MySql version: 5.5.38
got it working but there might be better ways

CREATE PROCEDURE simple6(IN name VARCHAR(20))
BEGIN

SET @sql_text = concat('SELECT endtime INTO @endtime1 FROM ',name,'big order by endtime DESC LIMIT 1');
PREPARE stmt FROM @sql_text;
EXECUTE stmt;

if @endtime1 IS NULL
then

SET @sql_text2=concat('INSERT INTO ',name,'big (starttime,endtime) SELECT starttime,endtime FROM ',name,' ORDER BY endtime DESC LIMIT 1');
PREPARE stmt2 FROM @sql_text2;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

else
SET @sql_text3= concat('set @cat:= (IF(TIMESTAMPDIFF(SECOND,(SELECT endtime from ',name,'big order by endtime DESC limit 1),(SELECT starttime from ',name,' order by starttime DESC limit 1)) &lt; 5,1,0))');
PREPARE stmt3 FROM @sql_text3;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;

if @cat !=0
THEN
SET @sql_text4=concat('UPDATE ',name,'big SET endtime = (SELECT endtime FROM ',name,' ORDER BY endtime DESC LIMIT 1) ORDER BY ID DESC LIMIT 1');
PREPARE stmt4 FROM @sql_text4;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;

else
SET @sql_text5=concat('INSERT INTO ',name,'big (starttime,endtime) SELECT starttime,endtime FROM ',name,' ORDER BY endtime DESC LIMIT 1');
PREPARE stmt5 FROM @sql_text5;
EXECUTE stmt5;
DEALLOCATE PREPARE stmt5;

end if;
end if;
DEALLOCATE PREPARE stmt;

END\\