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 `p`.`id`, `p`.`title`, `p`.`slug`, `p`.`label`, `p`.`price`, `p`.`old_price`, `p`.`category_id`, `p`.`picture_id`, `p`.`brand_id`
FROM `products` AS `p`
LEFT JOIN `brands` AS `b` ON `b`.`id` = `p`.`brand_id`
WHERE `p`.`title` LIKE '%лампа%' OR `b`.`title` LIKE '%лампа%' OR `b`.`tags` like '%лампа%'
GROUP BY `products`.`id` order by `products`.`sales` desc, `products`.`views` desc
LIMIT 18
SELECT `a`.*
FROM `articles` AS `a`
JOIN (
SELECT GROUP_CONCAT(DISTINCT `tag_id` ORDER BY `tag_id`) AS `tags`
FROM `articleTags`
WHERE `article_id` = :articleId
) AS `at`
JOIN (
SELECT GROUP_CONCAT(DISTINCT `tag_id` ORDER BY `tag_id`) AS `tags`, `article_id`
FROM `articleTags`
GROUP BY `article_id`
) AS `st` ON `st`.`article_id` = `a`.`id` AND `st`.`tags` = `at`.`tags`
SELECT *
FROM `items`
WHERE MATCH (`name`) AGAINST :searchString
OR `brand_id` IN (SELECT `id` FROM `brands` WHERE MATCH (`name`) AGAINST :searchString)
СREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a CHAR(16);
DECLARE b INT;
DECLARE cur CURSOR FOR SELECT id, data FROM t1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO a, b;
IF done THEN
LEAVE read_loop;
END IF;
здесь выполняются нужные действия
END LOOP;
CLOSE cur;
END