SELECT onesem_and_mdl_online_v2.*, forms_study_lang.name as form_name, forms_study_lang.order_num, from_unixtime(date_beg), from_unixtime(date_end),1 as order_old
FROM `onesem_and_mdl_online_v2`
inner join forms_study_lang on (forms_study_lang.form_study_id = onesem_and_mdl_online_v2.form_id and forms_study_lang.lang = 'ru-RU')
WHERE active_rasp = 1 and tag_name = 'Помощники' and (form_id IN (2))
and ((date_beg >= UNIX_TIMESTAMP(now()) or date_end >= UNIX_TIMESTAMP(now()))
or (not exists(select * from `onesem_and_mdl_online_v2` as cs where cs.uri = onesem_and_mdl_online_v2.uri
and cs.date_beg>onesem_and_mdl_online_v2.date_beg) and onesem_and_mdl_online_v2.cource>0))
and date_beg is not null and date_end is not null
union
SELECT onesem_and_mdl_ochno_v2.*, forms_study_lang.name as form_name, forms_study_lang.order_num, from_unixtime(date_beg), from_unixtime(date_end),
(case when (date_beg >= UNIX_TIMESTAMP(now()) or date_end >= UNIX_TIMESTAMP(now())) then 1 else 0 end) as order_old
FROM `onesem_and_mdl_ochno_v2`
inner join forms_study_lang on (forms_study_lang.form_study_id = onesem_and_mdl_ochno_v2.form_id and forms_study_lang.lang = 'ru-RU')
WHERE active_rasp = 1 and tag_name = 'Помощники'
and (form_id = 1)
and ((date_beg >= UNIX_TIMESTAMP(now()) or date_end >= UNIX_TIMESTAMP(now()))
or (not exists(select * from `onesem_and_mdl_ochno_v2` as cs where cs.uri = onesem_and_mdl_ochno_v2.uri
and cs.date_beg>onesem_and_mdl_ochno_v2.date_beg)))
and date_beg is not null and date_end is not null
ORDER BY order_num asc, order_old desc, (case when mid > 0 then date_beg else date_beg end) asc,
(case when mid > 0 then 10000000000-CONVERT(date_beg, UNSIGNED) else date_beg end) asc
Обучение Фэн шуй, Ба цзы, Ци Мэнь Дунь Цзя Помощники