-$ADMIN = " AND d.is_active='Y'";
-if (IS_ADMIN()) $ADMIN = '';
-$result = SQL_QUERY("SELECT DISTINCT d.id, d.admin_id, a.login, d.title, d.descr, d.template, d.start_time, d.end_time, d.min_users, d.min_prices
- FROM `{!_MYSQL_PREFIX!}_rallye_data` AS d, `{!_MYSQL_PREFIX!}_admins` AS a
- WHERE d.admin_id=a.id".$ADMIN." AND d.notified='Y'
- ORDER BY d.end_time LIMIT 1", __FILE__, __LINE__);
+$ADD = " AND d.`is_active`='Y'";
+if (isAdmin()) $ADD = '';
+
+$result = SQL_QUERY("SELECT
+ d.`id`,
+ d.`admin_id`,
+ a.`login`,
+ d.`title`,
+ d.`descr`,
+ d.`template`,
+ d.`start_time`,
+ d.`end_time`,
+ d.`min_users`,
+ d.`min_prices`
+FROM
+ `{?_MYSQL_PREFIX?}_rallye_data` AS d
+INNER JOIN
+ `{?_MYSQL_PREFIX?}_admins` AS a
+ON
+ d.`admin_id`=a.`id`
+WHERE
+ d.`notified`='Y'
+ " . $ADD . "
+ORDER BY
+ d.`end_time` ASC
+LIMIT 1", __FILE__, __LINE__);