select date, abs(sum-first_month_value)/2 from (select
sum(price) as sum,
count(date) as count,
date,
first_value(sum(price)) over() as first_month_value
from t
group by
date
order by date) as t;
select
year(date),
sum(`index`.`index`),
substring_index(group_concat(`index`.`index` order by date asc), ',', -1) as last
from
where id_uin = 4 and id_status = 5 group by year(date);
select
(select `index` from `index` where year(date) = '2018' and id_uin = 4 and id_status = 5 order by date desc limit 1 ) as '2018',
(select `index` from `index` where year(date) = '2019' and id_uin = 4 and id_status = 5 order by date desc limit 1 ) as '2019',
(select `index` from `index` where year(date) = '2020' and id_uin = 4 and id_status = 5 order by date desc limit 1 ) as '2020',
(select `index` from `index` where year(date) = '2021' and id_uin = 4 and id_status = 5 order by date desc limit 1 ) as '2021';
select
substring_index(group_concat(`2018`), ',', -1) as `2018`,
substring_index(group_concat(`2019`), ',', -1) as `2019`,
substring_index(group_concat(`2020`), ',', -1) as `2020`,
substring_index(group_concat(`2021`), ',', -1) as `2021`
from (SELECT
CASE WHEN date BETWEEN '2018-01-01' AND '2018-12-31' THEN `index` ELSE null END AS `2018`,
CASE WHEN date BETWEEN '2019-01-01' AND '2019-12-31' THEN `index` ELSE null END AS `2019`,
CASE WHEN date BETWEEN '2020-01-01' AND '2020-12-31' THEN `index` ELSE null END AS `2020`,
CASE WHEN date BETWEEN '2021-01-01' AND '2021-12-31' THEN `index` ELSE null END AS `2021`
FROM `index`
WHERE id_uin = 4 and id_status = 5
GROUP BY `2018`, `2019`, `2020`, `2021`) as t;
public function getPropAttribute(): string
{
return 'prop';
}
public function newInstance($attributes = [], $exists = false)
{
$model = parent::newInstance($attributes, $exists);
$model->setAppends($this->appends);
return $model;
}
(new \App\Models\User())->setAppends(['prop'])->newModelQuery()->get()->first()->toArray()
^ array:8 [
"id" => 2
"name" => "Алексеев Глеб Дмитриевич"
"email" => "fmarkov@example.com"
"email_verified_at" => "2022-06-16T02:18:03.000000Z"
"deleted_at" => null
"created_at" => "2022-06-16T02:18:03.000000Z"
"updated_at" => "2022-06-16T02:18:03.000000Z"
"prop" => "prop"
]
update event, (select id from event order by date limit 1) as s set value = 'changed', date = now() where t.id = s.id;
SELECT materials.id,
materials.title,
materials.author,
materials.description,
types.name AS type,
categories.name AS category
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
WHERE materials.author LIKE '$str%'
OR materials.title LIKE '$str%'
OR categories.name LIKE '$str%'
OR tags.name LIKE '$str%'
select materials.id,
materials.title,
materials.author,
materials.description,
types.name AS type,
categories.name AS category
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
where materials.id in (
SELECT materials.id
FROM materials
LEFT JOIN categories
ON materials.id_category = categories.id
LEFT JOIN types
ON materials.id_type = types.id
LEFT JOIN tags_to_materials
ON materials.id = tags_to_materials.material_id
LEFT JOIN tags
ON tags.id = tags_to_materials.tag_id
WHERE materials.author LIKE '$str%'
OR materials.title LIKE '$str%'
OR categories.name LIKE '$str%'
OR tags.name LIKE '$str%' group by materials.id);
select sum(distance) from (select
ST_Distance_Sphere(
point(points.longtitude, points.latitude),
(select
point(next.longtitude, next.latitude)
from
points as next
where
next.date > points.date
and
next.date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc limit 1)) as distance
from points
where date between '2022-07-01 00:00:00' and '2022-07-05 00:00:00'
order by date asc) as t;
SELECT user.*
FROM user
join
(
SELECT name, profession, city
FROM user
GROUP BY name, profession, city
HAVING COUNT(*) > 1
) as groupped on
groupped.name = user.name and COALESCE(groupped.profession = user.profession, 1) and COALESCE(groupped.city = user.city, 1)
ORDER BY name;