SELECT ID, Название, ВремяОт, ВремяДо
FROM Table
WHERE (ВремяОт > ВремяДо and ВремяОт >= '17:31:00' and ВремяДо <= '07:29:00') /* для обработки 1 и 2 строки */
or (ВремяОт < ВремяДо and (( ВремяОт >= '17:31:00' and ВремяДо <= '23:59:59' ) or ( ВремяОт >= '00:00:00' and ВремяДо <= '07:29:00' )) /* для обработки 5й строки */
SELECT `articles`.`id`, count(user_idea_likes.idea_id) AS likes,
count(subscribed_user_articles.article_id) AS subscribed
FROM `articles`
LEFT JOIN `user_article_likes` ON `user_article_likes`.`article_id` = `articles`.`id`
LEFT JOIN `subscribed_user_articles` ON `subscribed_user_articles`.`article_id` = `articles`.`id`
GROUP BY `articles`.`id`
SELECT DISTINCT a.name, b.position
FROM (SELECT name FROM employee) AS a CROSS JOIN (SELECT position FROM employee) AS b;
имен сотрудников некоторого отдела с разными должностями
SELECT *
FROM `poll`
INNER JOIN poll_answer ON poll.id = poll_answer.poll_id
INNER JOIN poll_result ON poll_answer.id = poll_result.poll_answer_id
WHERE CURDATE() BETWEEN poll.date_begin AND poll.date_end
AND poll_result.vote_ip ="222"
SELECT *
FROM `poll` as b
WHERE NOT EXISTS (SELECT poll.id
FROM `poll`
INNER JOIN poll_answer ON poll.id = poll_answer.poll_id
INNER JOIN poll_result ON poll_answer.id = poll_result.poll_answer_id
WHERE CURDATE() BETWEEN poll.date_begin AND poll.date_end
AND poll_result.vote_ip ="222"
AND b.id = poll.id)
select distinct a.id, a.value, (case when a.id_pc is not null then b.value when a.id_monitor is not null then c.value when a.id_printer is not null then d.value else null end) inv_num
from task a left join pc b on (a.id_pc=b.id)
left join monitor c on (a.id_monitor=c.id)
left join printer d on (a.id_printer=d.id)
select a.name
from mesta as a join mes_oso as b on (a.id = b.id_mesta)
join osobennosti as c on (b.id_os=c.id)
where c.name_os in ('wi_fi', 'coffee_to_go')
group by a.name
having count(*) = 2