-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.sql
More file actions
54 lines (46 loc) · 1.35 KB
/
task1.sql
File metadata and controls
54 lines (46 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Задание№1
START TRANSACTION;
INSERT INTO sample.users (name, birthday_at, created_at, updated_at)
select u.name, u.birthday_at, u.created_at, u.updated_at
from shop.users as u
where u.id =1;
COMMIT;
#Задание№2
use shop;
drop view if exists prod_view;
CREATE VIEW prod_view AS
select p.name as prod_name, c.name as cat_name
from products as p, catalogs as c
where p.catalog_id = c.id;
#Задание№3
use shop;
drop table if exists tb_create_at;
CREATE TABLE tb_create_at (
id SERIAL PRIMARY KEY,
created_at DATETIME
);
INSERT INTO tb_create_at (created_at) VALUES
('2018-08-01'),
('2018-08-04'),
('2018-08-16'),
('2018-08-17'),
('2018-08-19'),
('2018-08-21'),
('2018-08-22');
SELECT `x`.sequence_date, IF(`x`.sequence_date in (select created_at from tb_create_at),1,0) as `flag`
FROM (SELECT DATE_ADD('2018-08-01', INTERVAL `n`.`id` - 1 day) as sequence_date
FROM (SELECT @N := @N +1 AS id
FROM mysql.help_relation , (SELECT @N:=0) dum LIMIT 31) `n`
WHERE DATE_ADD('2018-01-01', INTERVAL `n`.`id` -1 DAY) <= '2018-12-30' ) x;
#Задание№4
use shop;
select created_at
into @search_date
from (select *
from tb_create_at as t1
order by t1.created_at desc
limit 5) as t2
order by t2.created_at
limit 1;
delete from tb_create_at as tb
where tb.created_at < @search_date;