select *
from 表 t,
(select id from 表 where begin = '$') t1,
(select id from 表 where id
not in (select id from 表 where begin = '$')) t2
where (t.id = t1.id and t.begin = '$') or t.id = t2.id
from 表 t,
(select id from 表 where begin = '$') t1,
(select id from 表 where id
not in (select id from 表 where begin = '$')) t2
where (t.id = t1.id and t.begin = '$') or t.id = t2.id
where id<>3
union
select id,begin,end from table_name
where id=3 and begin='$'