DELETE FROM table2
WHERE NOT EXISTS (SELECT id FROM table1 WHERE table2.id = table1.id);
SELECT city.name
FROM city
WHERE EXISTS (
SELECT city FROM ad_cars WHERE ad_cars.city = city.name
);
echo explode('/', $url,2)[1];
UPDATE employee SET salary = 5000 WHERE id = 1 and salary < 5000;
<?php
function param_val_add($pdo, $param_id, $obj_id, $value) {
if ($value) {
$query =
"INSERT INTO vals (
`param_id`, `obj_id`, `val`, `is_del`
) VALUES (
:param_id, :obj_id, :value, 0
) ON DUPLICATE KEY UPDATE
`is_del` = VALUES(is_del),
`val` = VALUES(val)";
$stmt = $pdo->prepare($query);
return $stmt->execute([
':param_id' => $param_id,
':obj_id' => $obj_id,
':value' => $value
]);
} else {
$query = "UPDATE vals SET `is_del` = 1 WHERE param_id = :param_id AND obj_id = :obj_id";
$stmt = $pdo->prepare($query);
return $stmt->execute([
':param_id' => $param_id,
':obj_id' => $obj_id
]);
}
}
param_val_add($pdo, 1, 1, 'value') ;
<?php
$data = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3]
];
$query="INSERT INTO t (a, b, c) VALUES " . implode(
', ',
array_fill(
0,
count($data),
'(' . implode (', ', array_fill(0, count($data[0]), '?')) . ')'
)
);
echo $query;
$values = array_reduce(
$data,
function($ac, $el) {
return array_merge($ac, array_values($el));
},
[]
);
var_export($values);
$q=$pdo->prepare($query);
$q->execute($values);
<?php
$phone = '1234567';
$sql = "INSERT INTO sportusers (userdate, phone)
SELECT NOW(), :userphone
WHERE NOT EXISTS (
SELECT 1 FROM sportusers s2
WHERE s2.phone = :userphone
AND userdate > date_sub(now(), interval 12 HOUR)
)";
$stmt = $pdo->prepare($sql);
$res = $stmt->execute([':userphone' => $phone]);
if($stmt->rowCount() > 0 ){
echo "Запись сделана";
} else {
echo "Try later";
}
with d as (
select
*,
row_number() over (partition by user_login order by issued_at) rn
from tbl
) select * from d where rn = 1;
select
coalesce(type::text, 'total by type') type,
coalesce(cost::text, 'total') cost,
sum(cost) as balance
from expenses e
where plan_date = '2023-12-04'
group by rollup (type,cost);