Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
select *
from blog
inner join (
select
from comment
where blog.id = comment.blog_id
order by comment.date_created desc
limit 10
) as comment on comment.blog_id = blog.id
where blog.type = 3
order by blog.date_created desc
limit 10
SELECT n.title, n.text, ..., c.author, c.comment, ...
FROM
(
SELECT news_id, title, text, ...
FROM news
WHERE type = ?
ORDER BY publication_date
) AS n
LEFT JOIN
(
SELECT news_id, author, comment, ...
FROM comments
ORDER BY creation_date DESC
LIMIT 10
) AS c
USING (news_id)
* This source code was highlighted with Source Code Highlighter.
select *
from (select * from news order by date desc limit 10) n
join comments c on (c.news_id = n.id)
where c.id in (
select id from comments c1 where c1.news_id = n.id order by c1.date desc limit 10
)
order by n.date desc, c.date desc
QUERY PLAN
Sort (cost=360399.12..360404.96 rows=2335 width=779) (actual time=163.127..163.213 rows=100 loops=1)
Sort Key: news.date, c.date
Sort Method: quicksort Memory: 117kB
-> Nested Loop (cost=0.00..360268.49 rows=2335 width=779) (actual time=1.005..162.394 rows=100 loops=1)
Join Filter: (subplan)
-> Limit (cost=0.00..0.56 rows=10 width=21) (actual time=0.024..0.060 rows=10 loops=1)
-> Index Scan Backward using ix_news_date on news (cost=0.00..1118.25 rows=20000 width=21) (actual time=0.021..0.039 rows=10
loops=1)
-> Index Scan using ix_comments_news_id on comments c (cost=0.00..204.58 rows=467 width=735) (actual time=0.013..0.111 rows=70 loo
ps=10)
Index Cond: (c.news_id = news.id)
SubPlan
-> Limit (cost=76.67..76.69 rows=10 width=12) (actual time=0.198..0.219 rows=9 loops=700)
-> Sort (cost=76.67..77.83 rows=467 width=12) (actual time=0.195..0.202 rows=9 loops=700)
Sort Key: c1.date
Sort Method: top-N heapsort Memory: 17kB
-> Index Scan using ix_comments_news_id on comments c1 (cost=0.00..66.58 rows=467 width=12) (actual time=0.012..0.10
5 rows=70 loops=700)
Index Cond: (news_id = $0)
Total runtime: 163.388 ms
select
*
from
(
select
*
from
news
order by
date desc
limit 10
) as `n`
inner join
(
select
id
from
comments
where
comments.number < = 10
) as com
on `n`.id = com.news_id
inner join
comments on `comments`.id = com.id
order by `n`.date desc, `comments`.date desc
* This source code was highlighted with Source Code Highlighter.Ну и на последок небольшая задачка
SELECT
`news`.`id`,
0 AS `news_id`,
`news`.`dt`,
FROM_UNIXTIME(`news`.`dt`),
`news`.`text`,
"News" AS `type`,
CONCAT(`news`.`dt`, "9999999999") AS `big_dt`
FROM `news`
WHERE `news`.`dt` >= (SELECT `dt` FROM `news` ORDER BY `dt` DESC LIMIT 9, 1)
LIMIT 10
UNION
SELECT
`news_comments`.`id`,
`news_comments`.`news_id`,
`news_comments`.`dt`,
FROM_UNIXTIME(`news_comments`.`dt`),
`news_comments`.`text`,
"Comment" AS `type`,
CONCAT(`news1`.`dt`, `news_comments`.`dt`) AS `big_dt`
FROM `news_comments`
INNER JOIN `news` `news1`
ON `news1`.`id` = `news_comments`.`news_id`
AND `news1`.`dt` >= (SELECT `dt` FROM `news` ORDER BY `dt` DESC LIMIT 9, 1)
WHERE `news_comments`.`dt` >= (
SELECT `c`.`dt` FROM `news_comments` `c` WHERE `c`.`news_id` = `news_comments`.`news_id` ORDER BY `c`.`dt` DESC LIMIT 9, 1
)
ORDER BY `big_dt` DESC
MySQL и JOINы