mysql - if condition inside a sql query -
following part of stored proceedure im using extract data db.
query
begin  set @sqlstring = concat("select b.id, c.name, c.accountid,, b.total_logs, a.time_start, a.time_end ,count(a.id) number_of_users    ",logtable," inner join users b on a.id = b.id inner join accounts c on b.accountid = c.accountid group id;");     prepare stmt @sqlstring;     execute stmt;  end at times in db, logtable(table passed in variable logtable_1, logtable_2 .... ) can non existent, when perticuler table missing crashes , throws error because a.time_start, a.time_end cannot have values without log table.
but want assign null on values a.time_start, a.time_end without throwing error,
so can body tell there way modify code like
begin  if logtable exists    \\ query  else    \\ query  end 
find existence of table querying information_schema.tables. if returns count equals 1 can proceed executing query on table. otherwise go else block.
sample:
declare table_exists int default 0;  select count(1) table_exists    information_schema.tables  table_schema='your_table_schema_name'    , table_name = 'your_table_name';  if table_exists   -- else   -- else end if; 
Comments
Post a Comment