SELECT
JSON_ARRAYAGG(
JSON_OBJECT(
'image_src', images.image_src,
'image_alt', images.image_alt
)
) image,
product.*
FROM product
LEFT JOIN images ON images.prod_id = product.id
GROUP BY
product.id,
product.name;
select id, test_date
from test
where extract(month from test_date) = 1;
create table questions (
id int primary key auto_increment,
question varchar(255)
);
create table question_options (
id int primary key auto_increment,
question_id int,
answer varchar(255)
);
select
q.question,
json_arrayagg(answer) answers
from questions q
join question_options qo on qo.question_id = q.id
group by q.id, q.question
;
+=============+============================+
| question | answers |
+=============+============================+
| Question 1? | ["Answer 1?", "Answer 2?"] |
+-------------+----------------------------+
| Question 2? | ["Answer 3?", "Answer 4?"] |
+-------------+----------------------------+
select
projects.id,
GROUP_CONCAT(if(day=1, name, null)) as Monday,
GROUP_CONCAT(if(day=2, name, null)) as Tuesday,
GROUP_CONCAT(if(day=3, name, null)) as Wednesday,
GROUP_CONCAT(if(day=4, name, null)) as Thursday,
GROUP_CONCAT(if(day=5, name, null)) as Friday,
GROUP_CONCAT(if(day=6, name, null)) as Saturday,
GROUP_CONCAT(if(day=7, name, null)) as Sunday
from projects
group by projects.id
;
+====+========+=========+===========+==========+========+==========+========+
| id | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday |
+====+========+=========+===========+==========+========+==========+========+
| 1 | John | | | | | | |
+----+--------+---------+-----------+----------+--------+----------+--------+
| 2 | | | | Jack,Mike| | | |
+----+--------+---------+-----------+----------+--------+----------+--------+
SELECT clients.name, SUM(balance*rate) as rub_balance
FROM clients
JOIN deposits d ON d.client_id = clients.id
JOIN currencies cur on d.currency = cur.id
GROUP BY clients.id, clients.name
;
SELECT
clients.name,
SUM(balance *
CASE d.currency
WHEN 'USD' THEN 75.64
WHEN 'EUR' THEN 91.67
ELSE 1
END
) as rub_balance
FROM clients
JOIN deposits d ON d.client_id = clients.id
$sql = "INSERT INTO `slider` (`title`, `description`, `link`, `type_event`, `date_select_1`, `date_select_2`, `date_select_3`, `date_select_4`, `date_cont_1`, `date_cont_2`, `date_select_1_time`, `date_select_2_time`, `date_select_3_time`, `date_select_4_time`, `date_cont_1_time`, `date_cont_2_time`, `title_en`, `description_en`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
select book_code, count(distinct bilet_number) lends
from lends
group by book_code
having count(distinct bilet_number) = (select count(distinct bilet_number) from abonents)
INSERT INTO tbl (id, chtoto) VALUES (74291910, array['string'])
ON CONFLICT (id) DO UPDATE SET chtoto=array_append(tbl.chtoto, 'string');
INSERT INTO tbl (id, chtoto) VALUES (74291910, array['string2'])
ON CONFLICT (id) DO UPDATE SET chtoto=array_append(tbl.chtoto, 'string2');
+==========+==================+
| id | chtoto |
+==========+==================+
| 74291910 | {string,string2} |
+----------+------------------+
select
i.name,
min(ru) filter (where fi.id = 218) key1,
min(ru) filter (where fi.id = 219) key2,
min(ru) filter (where fi.id = 220) key3
from items i
left join item_fields fi on i.id = fi.item_id and fi.id in (218, 219, 220)
group by i.name, item_id
+=======+========+========+========+
| name | key1 | key2 | key3 |
+=======+========+========+========+
| Item1 | Поле 1 | Поле 2 | Поле 3 |
+-------+--------+--------+--------+
SELECT `e`.*
FROM `event` `e`
LEFT JOIN `event` ON `e`.`event_id` = `event`.`id`
ORDER BY `e`.`position` = 0, `e`.`position`
LIMIT 10;
ORDER BY IFNULL(`e`.`position`, 0) = 0, `e`.`position`
SELECT * FROM (
SELECT
IDBrewery,
ProductCode,
ProductPrice,
PeriodDate,
ROW_NUMBER() OVER (PARTITION BY ProductCode ORDER BY PeriodDate DESC) RN
FROM Price
JOIN Period ON Period.PeriodID = Price.PeriodID
) LastPrice
WHERE RN = 1
;
<?php
$input = [
['stock_type' => 'tests'],
['stock_name' => 'test', 'one_more_key' => 'one_more_value']
];
$result = [];
foreach($input as $i) {
$result = array_merge($result, $i);
}
var_export($result);
sql_c = "INSERT INTO " + name + " (`date`,`time`,`timestamp`,`name`,`price`,`chng_1`,`chng_2`,`chng_3`,`chng_4`,`chng_5`,`chng_10`,`chng_15`,`chng_30`,`chng_60`,`chng_day`) VALUES %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
CREATE TABLE users (
id int primary key auto_increment,
name varchar(64),
status enum('single', 'married', 'divorced')
);
INSERT INTO users (name, status) VALUES
('Peter', 'single'),
('Basil', 'single'),
('Peter', 'married'),
('Basil', 'married'),
('Basil', 'divorced');
SELECT * FROM (
SELECT
name,
status,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) rn
FROM users) user_status
WHERE rn = 1;
CREATE TABLE platforms (
id serial,
name varchar(64),
date_added TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO platforms (name) VALUES ('Test platform');
SELECT * FROM platforms;
SELECT id, name, to_char(date_added, 'dd/mm/YYYY') FROM platforms;
// переформатируем авторов в формат email => author
$arr["authors"] = array_reduce(
$arr["authors"],
function($res, $el) {
$res[$el["email"]] = $el["fio"];
return $res;
},
[]
);
// в цикле генерируем HTML
foreach($arr["books"] as $book ) {
echo $book["nameBook"] .
', ее написал ' . $arr["authors"][$book["email"]] .
' email автора: ' . $book["email"] . PHP_EOL;
}