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
SELECT sc.*, (select forms_study_lang.name from forms_study_lang where form_study_id = sc.form_study and forms_study_lang.lang = 'ru-RU' limit 1) as form_name, sc.form_study as form_id, si.enable, sch_cources_tags.tag as tag_name
FROM `onesem_and_mdl_v2` as sc
INNER JOIN `sch_cources_tags` ON ((sch_cources_tags.sem_id = `sc`.id and sc.as_seminar=0 and sch_cources_tags.tag = 'Физиогномика') or (sch_cources_tags.mdl_id = `sc`.id and sc.as_seminar=2 and sch_cources_tags.tag = 'Физиогномика'))
LEFT JOIN `shop_items` si on si.id = sc.id_shop
WHERE sc.active_rasp = 1
and ((active_shop = 1 and (sc.form_study = 3 or sc.form_study = 4 or sc.form_study = 5 or sc.form_study = 6)) or
((active_shop = 1 or (sc.as_seminar = 0 and (select active_shop from sch_modules where id = sc.mid and active_rasp = 1)=1) or sc.form_study = 4) and sc.form_study = 4))
group by (case when sc.uri = '' then sc.id else uri end), sch_cources_tags.tag
ORDER BY FIELD( sch_cources_tags.tag,'Бацзы','Фэн шуй','Ци Мэнь Дунь Цзя','Выбор дат'), (select forms_study_lang.order_num from forms_study_lang where form_study_id = sc.form_study and forms_study_lang.lang = 'ru-RU' limit 1) asc, as_seminar desc, case when as_seminar = 2 then sc.cid when as_seminar in (0,1) then sc.date_beg end desc, (case when as_seminar = 2 then 10000000000-CONVERT(date_beg, UNSIGNED) else date_beg end) desc
Обучение Фэн шуй, Ба цзы, Ци Мэнь Дунь Цзя Физиогномика