select
portfolio.id, portfolio.name,
array_agg(json_build_object('src', photos.src, 'src_build', photos.src_build))
from portfolio
left join photos on portfolio.id = photos.portfolio_id
group by portfolio.id, portfolio.name
;
SELECT * FROM (
-- SELECT MOST EXPENSIVE PRODUCT
SELECT TOP(1) ProdID, Name
FROM Product
ORDER BY Price DESC
) ExpensiveProd
JOIN (
-- CALCULATE SALES BY PRODUCT IN DATE INTERVAL
SELECT ProdID, SUM(Sum) ProdSales
FROM Sales
WHERE Date BETWEEN '2019-07-01' AND '2019-07-31' GROUP BY ProdID
) ProdSales ON ProdSales.ProdID = ExpensiveProd.ProdID;
SELECT * FROM `action` WHERE `condition` LIKE '%"action": "6"%';
$prod = [
'x' => new Phone('Iphone X ', '$1500 ', 'Phone ', 'Apple ', 'A9 ', '1024 ', '1 ', '128gb ', 'Ios '),
'mon' => new Monitor('Abracadabra ', '$1500 ', 'Monitor ', 'DELL ', '24 ', '144ghz ', 'HDMI '),
'mon1' => new Monitor('ROG ', '$1500 ', 'Monitor ', 'ASUS ', '24 ', '144ghz ', 'HDMI ')
];
foreach($prod as $p) $p->show();
<?php
function cut($num, $remove_intervals) {
// sort remove intervals by first value
usort($remove_intervals, function($a, $b){return $a[0] <=> $b[0];});
// merge overlapped intervals
$remove = array_reduce(
$remove_intervals,
function($res, $el) {
$cnt = count($res)-1;
if ($el[0] <= $res[$cnt][1]) {
$res[$cnt][1] = max($el[1], $res[$cnt][1]);
} else {
$res[] = $el;
}
return $res;
},
[$remove_intervals[0]]
);
// build result array
$result = array_reduce(
$remove,
function($res, $el) use ($num) {
$cnt = count($res)-1;
if ($el[0] < $res[$cnt][1]) {
$res[$cnt][1] = $el[0];
if ($el[1] < $num) {
$res[] = [$el[1], $num];
}
}
return $res;
},
[[1, $num]]
);
var_export($result);
return $result;
}
print_r(cut(10000, [[30, 500], [700, 900], [800, 1000]]));
print_r(cut(10000, [[30, 500], [1200, 3700], [50, 700], [1000, 3000], [6000, 20000]]));
print_r(cut(1000, [[30, 500], [20, 7000], [10, 35]]));
$items = [
'school_table' => [
'name' => 'School Table',
'price' => 100
],
'school_chair' => [
'name' => 'School Chair',
'price' => 50
]
];
$item = $items[ 'school_table'];
echo $item['name'] . ' for ' . $item['price'] . ' money';
$values = ['AF', 'AL'];
$result = array_filter(
$countries,
function($el) use ($values) {
return in_array($el['iso'], $values);
}
);
select
posts.*,
json_objectagg(likers.user_id, likers.lname) lastnames,
json_objectagg(likers.user_id, likers.avatar) avatars
from posts
join (
select post_id, user_id, fname, lname, avatar, row_number() over (partition by post_id) rn
from likes
join users on users.id = likes.user_id
) likers on likers.post_id = posts.id
where likers.rn < 6
group by posts.id, posts.title, posts.description
;
<?php
$text = "Не следует, однако, забывать о том, что начало [повседневной] работы по [формированию позиции] влечет за собой [процесс внедрения и [модернизации] системы обучения кадров!";
preg_match_all("/\[(\S+)\]/", $text, $matches);
var_dump($matches[1]);
SELECT
orders.num, orders.createdon, orders.cost, orders.status,
GROUP_CONCAT(products.name) prod_names,
GROUP_CONCAT(products.count) prod_counts,
GROUP_CONCAT(products.cost) prod_costs
FROM `modx_ms2_orders` AS orders
JOIN `modx_ms2_order_products` AS products ON orders.id = products.order_id
WHERE `user_id` = 106
GROUP BY orders.num, orders.createdon, orders.cost, orders.status
<?php
$str1 = 'Привет';
$str2 = 'test';
$max_len = max(mb_strlen($str1),mb_strlen($str2));
echo str_pad(
$str1,
$max_len,
" ",
STR_PAD_BOTH
) . PHP_EOL ;
echo str_pad(
$str2,
$max_len,
" ",
STR_PAD_BOTH
) . PHP_EOL ;
select * from (
select
*,
date(from_unixtime(created_at)) created_date,
row_number() over (partition by created_date order by created_at desc) rn
from tbl) row_numbers where rn = 1
select tbl.* from tbl
join (
select max(created_at) max_created_at
from tbl
group by date(from_unixtime(created_at))
) last_time on last_time.max_created_at = tbl.created_at;
<?php
$string = 'This is my habr';
$pattern = '/(\w+ \w+)$/i';
$replacement = '<a href="habr.com">${1}<a>';
echo preg_replace($pattern, $replacement, $string);
SELECT "complex"."id", "complex"."rate", MIN("flat"."price_call") "min_flat_price_call"
FROM "flat"
LEFT JOIN "complex" ON "flat"."complex_id" = "complex"."id"
LEFT JOIN "building" ON "flat"."building_id" = "building"."id"
LEFT JOIN "section" ON "flat"."section_id" = "section"."id"
GROUP BY "complex"."id", "complex"."rate"
ORDER BY
"complex"."rate" DESC,
"min_flat_price_call",
"complex"."room_price_min"
LIMIT 30;
$last_city = ''; // initial empty value
foreach($cities as $city) {
if ($last_city != '') { // if value not empty show transit option
echo '<option value="'.$last_city.'-'.$city['name'].'">'.$last_city.'-'.$city['name'].'</option>'. PHP_EOL;
}
echo '<option value="'.$city['name'].'">'.$city['name'].'</option>' . PHP_EOL;
$last_city = $city['name']; // store value for next iteration
}
<?php
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
echo $i . '<br />';
flush();
ob_flush();
sleep(1);
}
echo 'End ...<br />';
?>