<?php
// Есть товар "Своя Кружка" 1"
$item = '"Своя Кружка" 1"';
// Мне нужно преобразовать в «Своя Кружка» 1'
$item_renamed = preg_replace('/"(\W+)" (\d+)"/m', "«$1» $2'", $item);
echo $item_renamed;
create table user_items (
user_id int,
item_id int,
ownership enum('changed','confirmed')
);
-- Let user 1 have item 100
insert into user_items values (1, 100, 'confirmed');
select * from user_items;
-- user 1 offer his item 100 to user 2
update user_items set user_id = 2, ownership = 'changed' where item_id = 100 and user_id = 1 and ownership = 'confirmed';
select * from user_items;
-- user 2 confirm ownership
update user_items set ownership = 'confirmed' where item_id = 100 and user_id = 2 and ownership = 'changed';
select * from user_items;
select
m.match_id,
min(opponent_id) as op1, max(opponent_id) as op2
from match_opponents op
inner join matches m on op.match_id = m.match_id
where m.status = "started"
group by m.match_id;
<?php
function split_name($name) {
return mb_ereg_replace_callback(
'([а-яa-z])([А-ЯA-Z])',
function($m) {
return $m[1] . ' ' . $m[2];
},
$name
);
}
echo split_name('Семенова ИринаВикторовна') . PHP_EOL;
echo split_name('Иванова ОльгаВикторовна') . PHP_EOL;
echo split_name('InessaIvanovna Oliynichenko') . PHP_EOL;
update `test` set `answer` = '{"test":["qwerty","\\"test\\""]}' ;
SELECT
COALESCE(name, 'Total') AS name,
SUM(salary * percent * 3) AS result
FROM salaries
GROUP BY name
WITH ROLLUP;
<?php
$query = "SELECT
COALESCE(name, 'Total') AS name,
SUM(salary * percent * :multiplayer) AS result
FROM salaries
GROUP BY name
WITH ROLLUP;";
$stmt = $pdo->prepare($query);
$stmt->execute(['multiplayer' => 3]);
$result = $stmt->fetchALL(PDO::FETCH_ASSOC);
var_export($result);
<?php
$str = 'Paris SG - Barcelona';
$teams = explode (' - ', $str);
var_export($teams);
array (
0 => 'Paris SG',
1 => 'Barcelona',
)
select *
from colors
order by
id = 3 desc, -- 3 in get
id in (5, 6) desc, -- 5 and 6 in session
popularity asc;
$result = mysqli_query($link, "SELECT acc_get_current_balance('@account_id') AS current_balance");
while ($row = mysqli_fetch_assoc($result)) {
echo "Current Balance : {$row['current_balance']} <br>";
}
select point, count(distinct guest_id) as guest_count
from sales
group by point;
with recursive free_icons (id) as (
select 1
union all
select id+1 from free_icons where id <= 40
)
select * from free_icons
where not exists (
select icon_id from icons where icon_id = free_icons.id
)
limit 1;
select icons.icon_id + 1
from icons
left join icons as next_icon on icons.icon_id + 1 = next_icon.icon_id
where next_icon.icon_id is null and icons.icon_id between 2 and 40
limit 1;
SELECT *
FROM `catalog_national_finals` as A
LEFT JOIN `voting_data_national_finals` as B ON (A.id = B.id_participant)
WHERE A.year = '$year' AND (B.id_user = '$id_user' OR B.id_user IS NULL)
create table users (
id int primary key auto_increment
);
create table books (
id int primary key auto_increment,
created timestamp,
index(created)
);
create table read_books (
user_id int,
book_id int,
primary key (user_id, book_id)
);
select * from books
where UNIX_TIMESTAMP() - created < 600 and
not exists (
select book_id
from read_books
where read_books.book_id = books.id and read_books.user_id = :user_id
)
limit 1;
<?php
require_once "mysitedb.php";
$query = "SELECT * FROM notes";
$select_note = mysqli_query($link, $query);
while ($note = mysqli_fetch_array($select_note)) {
echo $note["id"] . "<br>";
echo '<a href="comments.php?note=' . $note["id"] . '">' . $note["title"] . "</a>";
echo $note["created"], "<br>";
echo $note["article"], "<br>";
}
SELECT * FROM (
SELECT
product.*,
prices.price ,
ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY timestamp DESC) rn
FROM product
JOIN prices ON prices.product_id = product.id
) prices WHERE rn = 1
;
SELECT
expire_at,
last_check,
DATEDIFF(expire_at, last_check)
FROM tbl
WHERE DATEDIFF(expire_at, last_check) < 7;