Is this the right and efficient way to deal with MySQL query?
If you have two questions -- one needing times, one not needing them, then you should pick between two different queries.  That will be a lot simpler and cleaner.

For performance, have

INDEX(s_name, to_time)
INDEX(b_name, s_name, from_time)


(These two indexes should be pretty good; other indexes may work equally well.)