SELECT count(*) from `runs` where `date` >= unix_timestamp(curdate());
SELECT count(distinct client) from `runs` where `date` >= unix_timestamp(curdate());
SELECT COUNT(*) FROM (
SELECT
`client`, MIN(`date`) `first_run`
FROM `runs`
GROUP BY `client`
HAVING `first_run` >= unix_timestamp(curdate())
) `todays_first_runs`;
select distinct manufacturer
from spent s
join products p on p.id = s.product_id;
select
products.*,
case when bad_manufacturers.manufacturer is null then 'Хороший производитель' else 'Бракодел' end manufacturer_rate
from products
left join (
select distinct manufacturer
from spent s
join products p on p.id = s.product_id
) bad_manufacturers using (manufacturer)
SELECT distinct p1.`id`,
p1.`name`,
p1.`serial`,
case
when (select count(*)
from `products` as p2
join `spent` as s2 on p2.`id` = s2.`product`
and s2.`issued` = 1 -- какое значение в качестве некачественного продукта ?
where p2.`serial` = p1.`serial`) > 0
then 1
else 0
end as `spent`
from `products` as p1
SELECT * FROM `products`
LEFT JOIN (SELECT `product`, `price`,
row_number() over (partition by `product` order by `id` desc) as price_rank
from `prices`) as prc on `products`.`id` = prc.`product`
and prc.price_rank = 1
limit 10
SELECT * FROM `products`
LEFT JOIN (SELECT `product`, `price`,
IF(@prev_id <> `product`, @p_rank := 0, @p_rank),
@prev_id := `product`,
@p_rank := @p_rank + 1 as price_rank -- определяем partition
from `prices`,
(SELECT @p_rank := 0) p_rank, -- начальное значение ранга
(SELECT @prev_id := -1) prev_id -- начальное значение товара (не должен быть в диапазоне сущ. id)
order by `product` asc,
`id` desc -- устанавливаем сортировку partition
) as prc on `products`.`id` = prc.`product`
and prc.price_rank = 1
limit 10
data () {
return {
on: {password: '123', date: 123456},
}
},
n = 2
[0,1,1] => true
[1,1,0] => false
const arr = [
[0,0,0,0],
[1,0,0,0],
[0,1,0,0],
[0,0,0,0],
];
const n = 2;
const inRow = (arr, oneMax) => {
const { length } = arr;
if(oneMax > length) {
return false;
}
const oneCounter = {
'--': 0,
'||': 0,
'\\': {
top: 0,
bot: 0,
},
'//': {
top: 0,
bot: 0,
},
};
const check = (value, counter, key) => {
if(value === 1) {
counter[key]++;
if(counter[key] === oneMax) {
return true;
}
} else {
counter[key] = 0;
}
return false;
}
for(let i = 0; i < length; i++) {
for(let j = 0; j < length; j++) {
if(
check(arr[i][j], oneCounter, '--') ||
check(arr[j][i], oneCounter, '||')
) {
return true;
}
}
oneCounter['--'] = 0;
oneCounter['||'] = 0;
}
const maxDiags = length - n + 1;
const last = length - 1;
for(let i = 0, maxJ = length; i < maxDiags; i++, maxJ--) {
for(let j = 0; j < maxJ; j++) {
if(
check(arr[j][i + j], oneCounter['\\'], 'top') ||
check(arr[i + j][j], oneCounter['\\'], 'bot') ||
check(arr[j][last - (i + j)], oneCounter['//'], 'top') ||
check(arr[i + j][last - j], oneCounter['//'], 'bot')
) {
return true;
}
}
oneCounter['\\'].top = 0;
oneCounter['\\'].bot = 0;
oneCounter['//'].top = 0;
oneCounter['//'].bot = 0;
}
return false;
}
inRow(arr, n)
local clients = {
['count'] = {0, 0},
['control'] = {},
['data'] = {},
}
local function set_counter_for_new_indexes_to(t, id)
setmetatable(t, {
__newindex = function(self, key, value)
clients['count'][id] = clients['count'][id] + clients['count'][id]
rawset(t, key, value)
end
})
end
set_counter_for_new_indexes_to(clients['control'], 2)
set_counter_for_new_indexes_to(clients['count'], 1)
--[[
variant #2
]]
local clients =
{
['control'] = {},
['data'] = {},
}
--
-- автоматически добавляет параметр .count к таблице в которой создается новой поле
--
local function set_counter_for_new_indexes_to(t)
setmetatable(t, {
__newindex = function(self, key, value)
if not rawget(self, 'count') then rawset(self, 'count', 1)
else
rawset(self, 'count', rawget(self, 'count'))
end
rawset(t, key, value)
end
})
end
-- применяет счётчик полей к кождой подтаблице в таблице clients
for k, v in pairs(clients) do
if type(v) == "table" then continue end
set_counter_for_new_indexes_to(v)
end
--[[ VARIANT_#3 ]] local clients =
{
['control'] = {},
['data'] = {},
}
local function initialize_fields_counter(t)
if not t.table_fields_counters then
t.table_fields_counters = {} end
local ni_method = {__newindex = function(self, key, value)
local kname = t.table_fields_counters[self]
t.table_fields_counters[kname] =
(t.table_fields_counters[kname] or 0) + 1
rawset(t, key, value)
end}
for k, v in pairs (t) do
if type(v) == "table" then continue end
t.table_fields_counters[v] = k
setmetatable( v, ni_method )
end
end initialize_fields_counter(clients )
-- получить счётчик элементов .
clients.t.table_fields_counters[<название-ключа-таблицы>]
A and B or C
local res = ((i ~= k) and 2 or nil)