SELECT distinct
first_value(`m`.`message`) over (partition by sender_id, recipient_id ORDER BY `m`.`id` DESC) AS `message`,
`u`.`id`, `u`.`name`, `m`.`read` , recipient_id /*вывел чтоб было видно с кем он общался*/
FROM `mails` AS `m`
JOIN `users` AS `u`
ON `m`.`sender_id` = `u`.`id`
WHERE `m`.`recipient_id` = $user_id
SELECT DISTINCT events.id, events.name as event_name, events.date_start, events.date_end, events.country_id, events.description, events.day_1, events.day_2, events.month_1, events.month_2, events.year_1, events.year_2, events.is_present, links.links, images.images, countries.countries, countries.country_ids, country_to_cluster.year_start FROM events
LEFT JOIN (SELECT countries.id, GROUP_CONCAT(countries.id) as country_ids, event_to_country.country_id as country_id, event_to_country.event_id, GROUP_CONCAT(countries.name) as countries FROM countries LEFT JOIN event_to_country ON countries.id = country_id GROUP BY event_to_country.event_id) as countries ON events.id = countries.event_id
LEFT JOIN (SELECT event_id, GROUP_CONCAT(link) as links FROM links GROUP BY event_id) AS links ON events.id = links.event_id
LEFT JOIN (SELECT event_id, GROUP_CONCAT(name) as images FROM images GROUP BY event_id) AS images ON events.id = images.event_id
LEFT JOIN keywords ON events.id = keywords.event_id
LEFT JOIN country_to_cluster ON country_to_cluster.country_id = events.country_id AND events.year_1 >= coalesce(country_to_cluster.year_start,events.year_1) AND events.year_1 <= coalesce(country_to_cluster.year_end,events.year_1)
WHERE (country_to_cluster.cluster_id = :cluster_id)
ORDER BY events.year_1 ASC
LIMIT 30
SELECT n.id, n.text
FROM news AS n
JOIN news_tags AS nt ON nt.news_id = n.id
JOIN tags AS t1 ON t1.id = nt.tag_id AND t1.name in ("животные", "кроты" /*перечисляете свои теги*/)
group by n.id, n.text
having count(distinct t1.id) = 2 /*кол-во перечисленных тегов*/
SELECT
u.*,
ui.`image_name`,
(SELECT COUNT(il.`id`) FROM images_likes AS il WHERE `il`.image_id = `ui`.id) AS count_likes,
FROM users AS u
LEFT JOIN (SELECT id, user_id, image_name,
@row_num := CASE WHEN @row_num_val = user_id THEN @row_num + 1
WHEN (@row_num_val := user_id) IS NOT NULL THEN 1
END rn
FROM users_images, (SELECT @row_num := null, @row_num_val := null) AS x
ORDER BY user_id, id
--select row_number() over(partition by user_id order by id) rn, user_id , id, image_name
--from users_images
) AS ui ON ui.user_id = u.id and ui.rn <= 5
WHERE u.flag > 0
MERGE INTO users u
USING (VALUES($1, $2, $3)) v
ON v.column1 = u.steamid
WHEN NOT MATCHED
INSERT (balance, steamid, name, avatar) VALUES (0, v.column1, v.column2, v.column3)
WHEN MATCHED
UPDATE SET name =v.column2;
SELECT product_id, count(distinct field_id) cnt_f, count(distinct value_id) cnt_v
from product
where ( (field_id = 'f1' and value_id in ('v1', 'v2'))
or (field_id = 'f2' and value_id in ('v3', 'v4'))
)
group by product_id
having count(distinct field_id) = 2 and count(distinct value_id) = 2
SELECT t1.type_id, t1.name, coalesce(t01.id, t02.id) t0_id
FROM table1 t1
left join type1 t01 on (t1.type_id = t01.id and t1.type_id = type1)
left join type1 t02 on (t1.type_id = t02.id and t1.type_id = type2)
WHERE t1.data_id = ?
SELECT
wp_posts.ID,
wp_posts.post_title,
wc_product_meta_lookup.minPrice,
wc_product_meta_lookup.maxPrice
FROM
wp_posts
LEFT JOIN (
SELECT
product_id,
(min_price * GREATEST(1, coalesce(t2.meta_value*80,1), coalesce(t3.meta_value*80),1)) as minPrice,
(max_price * GREATEST(1, coalesce(t2.meta_value*80,1), coalesce(t3.meta_value*80),1)) as maxPrice
FROM
wp_wc_product_meta_lookup as t1
LEFT JOIN wp_postmeta as t2 ON t1.product_id = t2.post_id
AND t2.meta_key = 'convert_to_rub'
AND t2.meta_value = 1
JOIN wp_term_relationships tr ON (t1.product_id = tr.object_id AND tr.term_taxonomy_id IN ( 83, 85 ) )
LEFT JOIN wp_termmeta as t3 ON tr.term_taxonomy_id = t3.term_id
AND t3.meta_key = 'convert_to_rub'
AND t3.meta_value = 1
) wc_product_meta_lookup ON (
wp_posts.ID = wc_product_meta_lookup.product_id
)
WHERE
1 = 1
AND wp_posts.post_type = 'product'
AND wc_product_meta_lookup.minPrice > 0
--GROUP BY wp_posts.ID
ORDER BY wp_posts.post_name ASC
select p.*
from prices p,
(select currency_id, max(relevant_from) max_relevant_from
from price
where relevant_from <= NOW()
group by currency_id) p2
where p.currency_id = p2.currency_id and p.relevant_from = p2.max_relevant_from
select t1.*
from table t1
join (select nomer_zakaza, max(data_obnovleniya) max_DT, min(data_obnovleniya) min_DT
from table
group by nomer_zakaza) t2
on (t1.nomer_zakaza = t2.nomer_zakaza and t1.data_obnovleniya in (max_DT, min_DT))
order by t1.nomer_zakaza, t1.data_obnovleniya
select ClientName, count( ClientName) cnt_all_row, count( distinct ClientName) cnt_unique_CN
from table2 t2
join table1 t1 on (t1.ClientID = t2.ClientID)
where t2.Query = 'Data4'
group by ClientName
select t2.Query, count( distinct ClientName) cnt_unique_CN
from table2 t2
join table1 t1 on (t1.ClientID = t2.ClientID)
where t2.Query = 'Data4'
group by t2.Query
select max(Strength) max_St, min(Strength) min_St
from table
, JSON_TABLE(concat('[',JSON_Col,']'),
'$[*]'
COLUMNS(
NESTED PATH '$.Strength[*]' COLUMNS (Strength INT PATH '$') )
) as tt