SELECT
expire_at,
last_check,
DATEDIFF(expire_at, last_check)
FROM tbl
WHERE DATEDIFF(expire_at, last_check) < 7;
SELECT TOP (10) m.membershipId FROM Membership AS m;
SELECT TOP (10) m.[Member Name] FROM [Service Membership] AS m;
<?php
$arr =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
$res = array_filter (
$arr,
function($el) { return ($el % 2 == 0); }
);
print_r($res);
<?php
$update = 'UPDATE `bes`
SET
`many` = IF (`many` >= :cena, `many` - :cena, `many`),
`activation` = IF (`many` >= :cena, 1, 0)
WHERE `activation` = 1';
$stmt = $pdo->prepare($update);
$stmt->execute([':cena'=>3]);
<?php
$query = "SELECT MAX(id) AS max FROM test";
$stmt = $pdo->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$max_id = $row['max'];
$query = "SELECT * FROM test WHERE id >= ? LIMIT 1";
$stmt = $pdo->prepare($query);
$stmt->execute([rand(0, ($max_id-1))]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row);
if(!empty($masterSumm) && !empty($controlSumm)){
Возвращает false, если var существует и содержит непустое ненулевое значение или строку false, смотрите Преобразование в булев тип. В противном случае возвращает true.
SELECT user_id
FROM some_table
WHERE num IN (878, 925, 242)
GROUP BY user_id
HAVING COUNT(DISTINCT num) = 3 -- 3 количество вариантов
;
SELECT some_table.user_id
FROM some_table
JOIN some_table st1 ON st1.user_id = some_table.user_id AND st1.num = 925
JOIN some_table st2 ON st2.user_id = some_table.user_id AND st2.num = 242
WHERE some_table.num = 878
;
SELECT user_id, GROUP_CONCAT(DISTINCT num ORDER BY num)
FROM some_table
WHERE num IN (878,925,242)
GROUP BY user_id
HAVING GROUP_CONCAT(DISTINCT num) = '242,878,925'
;
// найдем ключи с равными значениями
$equal_keys = array_keys(array_intersect_assoc(...$array));
foreach ($array as $k=>$val) {
// в цикле отфильтруем лишнее
$array[$k] = array_filter(
$val,
function ($key) use ($equal_keys) {
return !in_array($key, $equal_keys);
},
ARRAY_FILTER_USE_KEY
);
}
SELECT `question`.*
FROM `question`
WHERE
`question`.`deleted` = 0 AND
`question`.`moderated` = 0 AND
`question`.`published_date` > 0 AND
NOT EXISTS (SELECT `id` FROM `comment` WHERE `question`.`id` = `comment`.`material_id`)
ORDER BY `question`.`published_date` DESC
LIMIT 15;
<?php
$sql = "SELECT id FROM chatsystem WHERE user1 = ? or user2 = ?";
$stmt = mysqli_stmt_init($connect);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "ss", $username, $username);
mysqli_stmt_execute($stmt);
$resultData = mysqli_stmt_get_result($stmt);
mysqli_stmt_close($stmt);
while ($row = mysqli_fetch_assoc($resultData)) {
var_export($row);
}
<?php
function split_number($num, $count) {
$result = [];
for ($i = 1; $i<$count; $i++) {
// random number between 1 and half of given number
$result[$i] = rand(1, $num / 2);
$num -= $result[$i];
}
$result[0] = $num;
return $result;
}
$chunks = split_number(500, 5);
<?php
$request = [
87=>'AMD',
86=>'8'
];
$filter = implode(' or ', array_fill(0, count($request), '(pv.property_id = ? and pv.value = ?)'));
$filter_values = [];
foreach($request as $key=>$val) {
$filter_values[] = $key;
$filter_values[] = $val;
}
$query = 'select p.* from products p
join property_values pv on p.id = pv.product_id
where ' . $filter . '
group by p.id, p.name
having count(distinct pv.id) = ' . count($request);
CREATE TABLE users (
id serial primary key,
name varchar(255)
);
INSERT INTO users VALUES (1, 'John'), (2, 'Sarah');
CREATE TABLE shares (
id varchar(12) primary key,
price numeric
);
INSERT INTO shares VALUES ('TSLA', 781.30), ('AAPL', 105.00);
CREATE TABLE user_shares (
user_id int,
share varchar(12),
count int
);
INSERT INTO user_shares VALUES (1, 'TSLA', 5), (2, 'AAPL', 1);
SELECT
users.*,
shares.*,
user_shares.count,
user_shares.count * shares.price total
FROM user_shares
JOIN users ON user_shares.user_id = users.id
JOIN shares ON user_shares.share = shares.id
;