SELECT `t2`.`id`, IFNULL(`t2`.`field1`, `t1`.`field1`), IFNULL(`t2`.`field2`, `t1`.`field2`), ...
FROM `table1` AS `t1`
LEFT JOIN `table2` AS `t2` ON `t2`.`id` = `t1`.`id` AND `t2`.`date` = :date
WHERE `t1`.`id` = :id
SELECT `c`.`date`, `t2`.`id`, IFNULL(`t2`.`field1`, `t1`.`field1`), IFNULL(`t2`.`field2`, `t1`.`field2`), ...
FROM `calendar` AS `c`
LEFT JOIN `table1` AS `t1` ON `t1`.`id` = :id
LEFT JOIN `table2` AS `t2` ON `t2`.`id` = :id AND `t2`.`date` = `c`.`date`
WHERE `c`.`date` BETWEEN :start_date AND :end_date
SET @id = 0;
UPDATE `test` SET `lock` = 1 WHERE @id := `id` AND `lock` = 0 ORDER BY `time` LIMIT 1;
SELECT @id;
SELECT `a`.`name`, IFNULL(`d`.`count`, 0) AS `count`
FROM `authors` AS `a`
LEFT JOIN (
SELECT `author`, COUNT(*) AS `count`
FROM `docs`
GROUP BY `author`
) AS `d` ON `d`.`author` = `a`.`id`
UNION (SELECT CONCAT('Неизвестный ', IFNULL(`d`.`author`, '')), `d`.`count`
FROM `authors` AS `a`
RIGHT JOIN (
SELECT `author`, COUNT(*) AS `count`
FROM `docs`
GROUP BY `author`
) AS `d` ON `d`.`author` = `a`.`id`
WHERE `a`.`id` IS NULL)
ORDER BY `count` DESC
SELECT `actor`.`name`
FROM `actor`
LEFT JOIN (
SELECT DISTINCT `id_actor`
FROM `film_actor`
) AS `infilm` ON `infilm`.`id_actor` = `actor`.`id`
WHERE `infilm`.`id_actor` IS NULL
SELECT `t1`.`name`, `t1`.`val`, `t1`.`date`
FROM `table` AS `t1`
JOIN (
SELECT `name`, MAX(`date`) AS `date`
FROM `table`
GROUP BY `name`
) AS `t2` ON `t2`.`name` = `t1`.`name` AND `t2`.`date` = `t1`.`date`
SELECT `t1`.`id`
FROM (SELECT DISTINCT `id` FROM `table` WHERE `cat` = 1) AS `t1`
JOIN (SELECT DISTINCT `id` FROM `table` WHERE `cat` = 2) AS `t2` ON `t2`.`id` = `t1`.`id`
JOIN (SELECT DISTINCT `id` FROM `table` WHERE `cat` = 10) AS `t3` ON `t3`.`id` = `t1`.`id`
SELECT `id`
FROM `table`
WHERE `cat` IN (1, 2, 10)
GROUP BY `id`
HAVING COUNT(`id`) >= 3