stations (id, name);
events (card, event, station_id, sum, timestamp)
SELECT `os`.`service_cost`
FROM `Org_services` AS `os`
JOIN `Organizations` AS `o` ON `o`.`ID` = `os`.`org_id` AND `o`.`org_name` = :orgName
JOIN `Services_list` AS `s` ON `s`.`ID` = `os`.`service_id` AND `s`.`service_name` = :servName
SELECT DATE(`date_insert`) AS `date`, SUM(`price`), AVG(`price`), COUNT(*)
FROM `b_sale_order`
WHERE (`canceled` = 'N') AND (`date_insert` BETWEEN :StartD AND :EndD)
GROUP BY `date`
ORDER BY `date`
SELECT `v`.`id_user`, `v`.`count`, ...
FROM (
SELECT `id_user`, COUNT(DISTINCT FROM_UNIXTIME(time_vizit, '%Y%m%d')) AS `count`
FROM `vizits`
WHERE `time_vizit` BETWEEN UNIX_TIMESTAMP(:startTime) AND UNIX_TIMESTAMP(:endTime)
AND `id_client` = :clientId
GROUP BY `id_user`
HAVING COUNT(*) > 2
) AS `v`
LEFT JOIN `users_hotspot` AS `uh` ON `uh`.`id_user` = `v`.`id_user`
SELECT `t`.`action_ID`, `t1`.`prov_title` AS `prov_title_A`, `t1`.`track_no` AS `track_no_A`,
`t2`.`prov_title` AS `prov_title_B`, `t2`.`track_no` AS `track_no_B`,
`t3`.`prov_title` AS `prov_title_C`, `t3`.`track_no` AS `track_no_C`
FROM (
SELECT DISTINCT `action_ID`
FROM `action_has_providers`
) AS `t`
LEFT JOIN `action_has_providers` AS `t1`
ON `t1`.`action_ID` = `t`.`action_ID` AND `t1`.`prov_title` = 'ProvA'
LEFT JOIN `action_has_providers` AS `t2`
ON `t2`.`action_ID` = `t`.`action_ID` AND `t2`.`prov_title` = 'ProvB'
LEFT JOIN `action_has_providers` AS `t3`
ON `t3`.`action_ID` = `t`.`action_ID` AND `t3`.`prov_title` = 'ProvC'
CREATE FUNCTION SearchRoot(id INT)
BEGIN
DECLARE parent INT;
SET parent = id;
REPEAT
SET id = parent;
SET parent = (SEARCH `id_parent` FROM `table` WHERE `id` = id);
UNTIL parent > 0 END REPEAT;
RETURN id;
END