SELECT `c`.`id` AS `city_id`, `u`.`id` AS `url_id`, `u`.`url` AS `url`
FROM (
SELECT `id`, `lastCheckTimestamp`
FROM `cities`
ORDER BY `lastCheckTimestamp`
LIMIT 1
) AS `c`
LEFT JOIN `urls` AS `u` ON `u`.`city_id` = `c`.`id`
AND `u`.`lastCheckTimestamp` < `c`.`lastCheckTimestamp`
LIMIT 1
SELECT *
FROM `cars` AS `c`
JOIN `cars_attr` AS `ca1` ON `ca1`.`id_car` = `c`.`id`
AND `ca1`.`id_attr` = 4 AND `ca1`.`val` >= 2004 AND `ca1`.`val` <= 2008
JOIN `cars_attr` AS `ca2` ON `ca2`.`id_car` = `c`.`id`
AND `ca1`.`id_attr` = 3 AND `ca1`.`val` >= 10000 AND `ca1`.`val` <= 90000
JOIN `cars_attr` AS `ca` ON `ca`.`id_car` = `c`.`id`
JOIN `attr` AS `a` ON `a`.`id` = `ca`.`id_attr`
WHERE `c`.`category` = 1
SELECT *
FROM `cars` AS `c`
JOIN `cars_attr` AS `ca` ON `ca`.`id_car` = `c`.`id`
JOIN `attr` AS `a` ON `a`.`id` = `ca`.`id_attr`
WHERE `c`.`category` = 1
AND `c`.`id` IN (
SELECT `id_car` FROM `cars_attr`
WHERE `ca1`.`id_attr` = 4 AND `ca1`.`val` >= 2004 AND `ca1`.`val` <= 2008
) AND `c`.`id` IN (
SELECT `id_car` FROM `cars_attr`
WHERE `ca1`.`id_attr` = 3 AND `ca1`.`val` >= 10000 AND `ca1`.`val` <= 90000
)