select regexp_replace(
'1 (111) 111-11-11,1 (111) 111-11-11,1 (111) 111-11-11,1 (111) 111-11-11,1 (111) 111-11-11',
',1',
',8'
) replaced from dual;
$favorites = [103, 105];
$favorite_ids = implode(',', $favorites);
$query = sprintf('SELECT * FROM employees WHERE employee_id IN (%s)', $favorite_ids);
select * from activity order by id asc limit 0, 3;
select * from activity order by id asc limit 3, 3;
SELECT
p.*
FROM
products as p
WHERE
EXISTS (
SELECT 1 FROM product_filter pf WHERE pf.filter_id = 1 AND pf.product_id = p.id
)
AND EXISTS (
SELECT 1 FROM product_filter pf WHERE pf.filter_id = 2 AND pf.product_id = p.id
);
with order_files as (
select
*,
row_number() over (partition by filename order by version desc, created_at desc) rn
from files
where user_id = 1
) select id, filename, user_id, version, created_at
from order_files where rn = 1
;
<?php
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("CREATE TABLE `geolocal` (
`id` INT AUTO_INCREMENT,
`names` VARCHAR(50),
`pass` VARCHAR(20),
`email` VARCHAR (50),
PRIMARY KEY(`id`)
)"
);
$mysqli->query(
"INSERT INTO `geolocal` (`names`, `pass`, `email`) VALUES ('spiderman', 'pautinka', 's.pider@list.ru')"
);
$mysqli->close();
create table customers (
id int primary key auto_increment,
name varchar(128)
);
create table products (
id int primary key auto_increment,
name varchar(128),
price decimal(9, 2)
);
create table baskets (
id int primary key auto_increment,
user_id int references customers(id),
product_id int references products(id),
price decimal(9, 2),
amount decimal(6, 3)
);
-- get records with wrong parents
select *
from test
where
parent_id is not null
and not exists (
select id from test as parents where parents.id = test.parent_id
);
-- delete records with wrong parents
delete test.*
from test
left join test as parents on parents.id = test.parent_id
where test.parent_id is not null and parents.id is null;
select * from test;
-- add foreign key to prevent missing parents
alter table test add foreign key (parent_id) references test(id);
WITH d AS (
SELECT
*,
row_number() over (partition by key order by created desc) rn
FROM move_history
) SELECT *
FROM d
WHERE status = 'COMPLETED'
AND rn = 1
ORDER BY id;