create table brands (
id int auto_increment primary key,
name varchar(64)
);
create table products (
code varchar(8) primary key,
brand_id int,
name varchar(64)
);
create table product_sizes (
product_code varchar(8),
size varchar(64),
quantity int,
price decimal(7,2)
);
$asr_res = "первое";
if (preg_match('/(первое|первае|первуе)/', $asr_res)) {
$ress = 1;
}
SELECT
`t1`.*,
GROUP_CONCAT(`t3`.`name` SEPARATOR ', ') AS `role_group`
FROM
`user` AS `t1`
JOIN `user_role` AS `t2`
ON
`t1`.`id` = `t2`.`user_id`
JOIN `access_group` AS `t3`
ON
`t2`.`group_id` = `t3`.`id`
GROUP BY
`t1`.`id`
HAVING SUM(`group_id` IN('20', '24')) = 2
LIMIT 0, 20
SELECT
book.NAME,
SUM(IF(store.CITY = 'Moscow', QUANTITY, 0)) AS CITY1_QUANTITY,
SUM(IF(store.CITY = 'London', QUANTITY, 0)) AS CITY2_QUANTITY,
SUM(IF(store.CITY = 'Moscow', QUANTITY, 0)- IF(store.CITY = 'London', QUANTITY, 0)) AS DIFF_QUANTITY
FROM book
LEFT JOIN book_store ON book_store.BOOK_ID = book.ID
JOIN store ON store.ID = book_store.STORE_ID
WHERE store.CITY IN ('Moscow', 'London')
GROUP BY book.NAME;
$get_product = $db->prepare("SELECT * FROM products");
$get_product->execute();
// объявление массива
$product_data_array = [];
while ($product_data = $get_product->fetch()) {
// добавляем массив как елемент 2-мерного массива
$product_data_array[] = [
"name" => $product_data["name"],
"price" => $product_data["price"],
"status" => $product_data["status"],
];
}
var_dump($product_data_array);
$get_product = $db->prepare("SELECT * FROM products");
$get_product->execute();
$product_data_array = $get_product->fetchAll(PDO::FETCH_ASSOC);
var_dump($product_data_array);
<?php
$date = new DateTime();
$date->setTimestamp(1644537600000/1000);
echo $date->format('Y-m-d') . "\n";
UPDATE first_table
JOIN second_table ON first_table.id = second_table.id
SET first_table.count = first_table.count + second_table.total_count, second_table.total_count = 0
WHERE second_table.id = 1;
SELECT
`table1`.`model`,
`table1`.`quantity`,
SUM(table2.sale_quantity)
FROM `table1`
JOIN `table2` ON `table1`.`model`=`table2`.`model`
WHERE
`table1`.`manufacturer_id` = 'apple'
AND `table1`.`quantity` < 20
GROUP BY `table1`.`model`, `table1`.`quantity`;
create table datavalues (
id serial primary key,
value text -- json or any suitable type
);
create table manydata (
id bigserial primary key,
value_id int,
inserted_at timestamp with time zone default current_timestamp,
updated_at timestamp,
foreign key (value_id) references datavalues(id)
);
create index on manydata(value_id);
SELECT
`users`.`name`,
COUNT(`orders`.`id`) as counts,
COALESCE(SUM(`orders`.`amount`), 0) as sums
FROM
`users`
LEFT JOIN `orders` ON
`users`.`id` = `orders`.`user_id` AND
`orders`.`date` BETWEEN '2021-03-29' AND '2021-03-30'
GROUP BY
`users`.`id`, `users`.`name`;
<?php
$results = [
['order_id' => 'first', 'id'=>1],
['order_id' => 'second', 'id'=>4],
['order_id' => 'third', 'id'=>7]
];
$filtered = array_filter(
$results,
function($el) {
return $el['id'] != 4;
}
);
var_export($filtered);
<?php
$word = "Lorem ipsum dolor sit amet consectetur adipisicing elit.";
$wordarr = str_split($word);
$converted_letters = array_map(
function($i, $l) {
return $i % 2 == 0 ? strtoupper($l) : strtolower($l);
},
array_keys($wordarr),
$wordarr
);
echo implode($converted_letters);
CREATE TABLE Cities (
id int primary key auto_increment,
city varchar(64)
);
CREATE TABLE CityDistnce (
city1 int,
city2 int,
distance int,
foreign key (city1) references Cities(id),
foreign key (city2) references Cities(id),
index (city1, city2)
);
CREATE TABLE abonents (
id serial,
name varchar(64),
address varchar(64),
phone varchar(64)
);
INSERT INTO abonents (name, address, phone) VALUES ('Petrov', 'Pobeda 9, 1', '8909999231');
SELECT * FROM abonents;