Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
SELECT
gal.*,
(
SELECT img.id FROM photo_image as img WHERE img.g_id = gal.id
ORDER BY img.is_main_foto DESC, img.ordi LIMIT 1
) as main_photo_id
FROM
photo_category as cat
INNER JOIN
photo_gallery as gal
ON
gal.c_id = cat.id
WHERE
cat.id = $catId
AND
cat.is_published = 1
AND
gal.is_published = 1
HAVING
main_photo_id IS NOT NULL
ORDER BY
gal.ordi
SELECT gal . * , (
SELECT id
FROM photo_image im
WHERE im.g_id = gal.id
AND im.is_published = TRUE
ORDER BY im.is_main_foto, im.ordi
LIMIT 1
)im_id
FROM photo_gallery gal
WHERE gal.c_id = ?
AND gal.is_published = TRUE
HAVING im_id IS NOT NULL
ORDER BY gal.ordi
* This source code was highlighted with Source Code Highlighter.SELECT
img.*
FROM
photo_image as img
WHERE
img.ordi > ( SELECT img2.ordi FROM photo_image as img2 WHERE img2.id = ? LIMIT 1)
AND
img.is_published = 1
AND
img.g_id = ?
LIMIT 1
select
( select i.id
from photo_image i
where i.is_published = 1
and i.g_id = :g_id
and i.ordi < (select x.ordi from photo_image x where x.id = :id)
limit 1
) as prv,
( select i.id
from photo_image i
where i.is_published = 1
and i.g_id = :g_id
and i.ordi > (select x.ordi from photo_image x where x.id = :id)
limit 1
) as nxt;
* This source code was highlighted with Source Code Highlighter.select
lag(i.id, 1) over (order by ordi) prv,
lead(i.id, 1) over (order by ordi) nxt
from photo_image i
where i.is_pubilshed = 1
and i.g_id = :g_id
and i.id = :id;
* This source code was highlighted with Source Code Highlighter.SELECT *
FROM
image LEFT JOIN folder USING (folder_id)
LEFT JOIN cathegory USING (cathegory_id)
WHERE
image.folder_id = (SELECT folder_id FROM images WHERE image_id=@image_id) AND
folder.is_published = 1 AND
cathegory.is_published = 1
ORDER BY
ABS(ordi - (SELECT ordi FROM images WHERE image_id=@image_id))
LIMIT 2;
* This source code was highlighted with Source Code Highlighter.SELECT *
FROM
image JOIN folder USING (folder_id)
JOIN cathegory USING (cathegory_id)
WHERE
image.folder_id = (SELECT (@folder_id:=folder_id) FROM images WHERE image_id=@image_id) AND
image.is_published = 1 AND
folder.is_published = 1 AND
cathegory.is_published = 1
ORDER BY
ABS(ordi - (SELECT (@ord := ordi) FROM images WHERE image_id=@image_id))
LIMIT
SIGN((SELECT count(ordi) FROM images WHERE folder_id=@folder_id AND ordi>@ord))
+
SIGN((SELECT count(ordi) FROM images WHERE folder_id=@folder_id AND ordi<@ord));
* This source code was highlighted with Source Code Highlighter.SELECT images.*,
(SELECT count(ordi) FROM images WHERE folder_id=(SELECT (@folder_id:=folder_id) FROM images WHERE image_id=@image_id) AND ordi>(SELECT (@ord := ordi) FROM images WHERE image_id=@image_id)) as next,
(SELECT count(ordi) FROM images WHERE folder_id=(SELECT (@folder_id:=folder_id) FROM images WHERE image_id=@image_id) AND ordi<(SELECT (@ord := ordi) FROM images WHERE image_id=@image_id)) as prev
FROM
image JOIN folder USING (folder_id)
JOIN cathegory USING (cathegory_id)
WHERE
image.folder_id = @folder_id AND
image.is_published = 1 AND
folder.is_published = 1 AND
cathegory.is_published = 1
ORDER BY
ABS(images.ordi - @ord)
LIMIT 2;
* This source code was highlighted with Source Code Highlighter.SELECT images.*,
(images.ordi - (SELECT (@ord:=ordi) FROM images WHERE image_id=@image_id)) as destination
FROM
image JOIN folder USING (folder_id)
JOIN cathegory USING (cathegory_id)
WHERE
image.folder_id = (SELECT (@folder_id:=folder_id) FROM images WHERE image_id=@image_id) AND
image.is_published = 1 AND
folder.is_published = 1 AND
cathegory.is_published = 1
ORDER BY
ABS(ordi - @ord)
LIMIT 2;
* This source code was highlighted with Source Code Highlighter.select g.id, g.title, i.id, i.title
from photo_category c
inner join photo_gallery g
on g.c_id = c.id
inner join photo_image i
on i.g_id = g.id
inner join photo_image i_1
on i_1.g_id = i.g_id
where (i.is_main_foto = 1
or i.is_published = 1)
and i_1.is_main_foto = 0
and i_1.is_published = 1
and g.is_published = 1
and c.is_published = 1
and c.id = :id
group by g.id, g.title, i.id, i.title
having count(i.id) > 1
or (count(i.id) <= 1
and count(i_1.id) > 0)
order by
g.ordi,
i.ordi;
* This source code was highlighted with Source Code Highlighter.SELECT
c_id
, g_id
, (
SELECT id
FROM photo_image p
WHERE
is_published = 1
AND p.g_id = yy.g_id
ORDER BY is_main_foto DESC, ordi
LIMIT 1
) p_id
FROM
(
SELECT
c.id c_id
, g.id g_id
, IF(
@typex = g.c_id
, @rownum := @rownum + 1
, @rownum := 1 + LEAST(0, @typex := g.c_id)
) AS rown
FROM
photo_category AS c
INNER JOIN photo_gallery AS g ON g.c_id = c.id
, (SELECT @rownum := 1, @typex := '_') zz
WHERE
c.is_published = 1
AND g.is_published = 1
ORDER BY c.ordi, g.ordi DESC
) yy
WHERE rown <= 5
* This source code was highlighted with Source Code Highlighter.select g.id, ifnull(
(select id from photo_image i where i.i.g_id=g.id and is_main_foto=1),
(select id from photo_image i where i.i.g_id=g.id and is_published=1 limit 1)
) as photo_id
from photo_gallery g
where g.c_id = ...
and g.is_published = 1
and exists(select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
order by g.ordiselect (select i2.id from photo_image i2
where i1.g_id = i2.g_id and i2.ordi > i1.ordi
and (i2.is_published = 1 or i2.is_main_foto = 1)
order by i2.ordi asc limit 1) as next_id,
(select i2.id from photo_image i2
where i1.g_id = i2.g_id and i2.ordi < i1.ordi
and (i2.is_published = 1 or i2.is_main_foto = 1)
order by i2.ordi desc limit 1) as prev_id
from photo_image i1 where i1.id = ...select c.id as c_id, g.id as g_id, ifnull(
(select id from photo_image i where i.i.g_id=g.id and is_main_foto=1),
(select id from photo_image i where i.i.g_id=g.id and is_published=1 limit 1)
) as photo_id
from photo_category c
join photo_gallery g on g.c_id = c.id
where c.is_published=1 and g.is_published=1
and exists(select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
and g.id in (select g2.id from photo_gallery g2 where g2.c_id = c.id order by ordi desc limit ...)
order by c.ordi desc, g.ordi descselect c.id as c_id, g.id as g_id, ifnull(i.id,
(select id from photo_image i where i.i.g_id=g.id and is_published=1 limit 1)
) as photo_id
from (select id, (select min(ordi) as min_ordi
from (select ordi from photo_gallery g
where g.c_id = c.id and g.is_published=1
and exists (select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
order by ordi desc limit ...))
from photo_category c where c.is_published=1) c
join join photo_gallery g on g.c_id = c.id and g.is_published=1 and g.ordi >= min_ordi
and exists (select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
left join photo_image i on i.g_id=g.id and is_main_foto=1
order by c.ordi desc, g.ordi descselect id, (select ordi from photo_gallery g
where g.c_id = c.id and g.is_published=1
and exists (select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
order by ordi desc limit 5, 1) x
from photo_category c where c.is_published=1select c.id as c_id, g.id as g_id, ifnull(i2.id,
(select id from photo_image i3 where i3.g_id=g.id and is_published=1 limit 1)
) as photo_id
from (
select c.id, c.ordi, (select ordi from photo_gallery g
where g.c_id = c.id and g.is_published=1
and exists (select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
order by ordi desc limit ..., 1) min_ordi
from photo_category c where c.is_published=1
) c
join photo_gallery g on g.c_id = c.id and g.is_published=1
and (g.ordi > min_ordi or min_ordi is null)
and exists (select 1 from photo_image i where i.g_id=g.id and i.is_published=1)
left join photo_image i2 on i2.g_id=g.id and i2.is_main_foto=1
order by c.ordi desc, g.ordi descselect pg.id as PG_ID, pim.*, count(pim.id) as PI_IMG_NUMBER from photo_gallery pg inner join photo_image pim on (pg.id=pim.g_id) where pg.c_id=CATEGORY_ID and pg.is_published=1 group by pg.id having (PI_IMG_NUMBER > 0) order by pg.ordi, pim.is_main_foto DESC, pim.is_published DESC, pim.ordi
select pim.* from photo_image pim cross join photo_image pim2 on (pim.g_id=pim2.g_id) where pim2.ordi > pim.ordi and pim2.is_published=1 and pim.id=IMAGE_ID limit 0, 1
select pim.* from photo_image pim cross join photo_image pim2 on (pim.g_id=pim2.g_id and pim.g_id=GALLERY_ID) where pim2.ordi > pim.ordi and pim2.is_published=1 and pim.id=IMAGE_ID limit 0, 1
select pg.id as PG_ID, pig.* from photo_gallery pg inner join photo_image pim on (pg.id=pim.g_id) where pg.c_id='1' and pg.is_published=1 and (pim.is_published=1 or pim.is_main_foto=1) group by pg.id order by pg.ordi, pim.is_main_foto DESC, pim.is_published DESC, pim.ordi
select pim2.* from photo_image pim cross join photo_image pim2 on (pim.g_id=pim2.g_id and pim.g_id=GALLERY_ID) where pim2.ordi > pim.ordi and pim2.is_published=1 and pim.id=IMAGE_ID order by pim2.ordi ASC limit 0, 1
select pim2.* from photo_image pim cross join photo_image pim2 on (pim.g_id=pim2.g_id and pim.g_id=GALLERY_ID) where pim2.ordi < pim.ordi and pim2.is_published=1 and pim.id=IMAGE_ID order by pim2.ordi DESC limit 0, 1
where pim2.ordi < pim.ordi and pim2.is_published=1 and pim.id=IMAGE_ID and NOT(pim.id=pim2.id)
Три интересные задачи на знание SQL