Читал, что при использовании JOIN индексирование бесполезно
function russianDate(int $timestamp) {
list($year, $month, $day) = explode("-", date("Y-n-j", $timestamp));
$monthsHandbook = [
1 => "января",
2 => "февраля",
3 => "марта",
4 => "апреля",
5 => "мая",
6 => "июня",
7 => "июля",
8 => "августа",
9 => "сентября",
10 => "октября",
11 => "ноября",
12 => "декабря"
];
$monthName = $monthsHandbook[$month];
return "{$day} {$monthName} {$year} года";
}
WHERE
id
>=1 AND id <= 10000
WHERE id >=1 AND id <= 10000
UPDATE table SET field=field*2 WHERE id >=1 AND id <= 10000
SELECT
`uid`,
MAX(`utm`) AS 'utm'
FROM
`table`
GROUP BY
`uid`
SELECT
`uid`,
LEFT(GROUP_CONCAT(`utm` ORDER BY id DESC), LOCATE(',', GROUP_CONCAT(`utm` ORDER BY id))-1) AS 'utm'
FROM
`table`
GROUP BY
`uid`
SELECT
`uid`,
GROUP_CONCAT(`utm` ORDER BY id DESC LIMIT 1) AS 'utm'
FROM
`table`
GROUP BY
`uid`
SELECT
`table_b`.*
FROM
`table_b`
JOIN `table_a` ON (FIND_IN_SET(`table_b`.`id`, `table_a`.`keys`) > 0)
WHERE
`table_a`.`id`='8'
SELECT
`users`.*,
(SELECT
`actions`.`date`
FROM
`actions`
WHERE
`actions`.`user`=`users`.`id`
ORDER BY
`date` DESC
LIMIT 1) AS
'last_action_date'
FROM
`users`
WHERE
`id` = 1
SELECT
`users`.*,
(SELECT
JSON_OBJECT(
'date', `actions`.`date`,
'action', `actions`.`action`
)
FROM
`actions`
WHERE
`actions`.`user`=`users`.`id`
ORDER BY
`date` DESC
LIMIT 1) AS
'last_action'
FROM
`users`
WHERE
`id` = 1
service mysql stop
killall -KILL mysql mysqld_safe mysqld
service mysql start
SELECT
`table_products`.`name` AS 'name',
`table_warehouse_products`.`quantity` AS 'quantity',
`table_products`.`price` AS 'price',
`table_warehouse_products`.`quantity`*`table_products`.`price` AS 'total'
FROM
`table_products`
LEFT JOIN `table_warehouse_products` ON (`table_warehouse_products`.`product_id`=`table_products`.`id`)
CREATE TABLE `ticket` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`assignee_id` int DEFAULT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`subject` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`body` text COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `ticket_assignment_log` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`ticket_id` int unsigned NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`old_assignee_id` int DEFAULT NULL,
`new_assignee_id` int NOT NULL,
PRIMARY KEY (`id`),
KEY `i_ticket` (`ticket_id`),
CONSTRAINT `fk_ticket` FOREIGN KEY (`ticket_id`) REFERENCES `ticket` (`id`) ON UPDATE CASCADE
)
SELECT * FROM `table` WHERE FIND_IN_SET('28', `users_id`) > 0
SELECT * FROM `table` WHERE (`users_id` LIKE '28,%' OR `users_id` LIKE '%,28,%' OR `users_id` LIKE '%,28')
SELECT
## product_id
CASE
WHEN COUNT(`products`.`product_id`)=1 THEN ANY_VALUE(`products`.`product_id`)
ELSE (
SELECT
`product_id`
FROM
`products` AS `product_id_sub`
WHERE 1
AND `product_id_sub`.`group_id`=`products`.`group_id`
AND `product_id_sub`.`quantity` > `product_id_sub`.`minimum`
ORDER BY
`product_id_sub`.`minimum` DESC
LIMIT 1
)
END AS 'product_id',
## name
CASE
WHEN COUNT(`products`.`product_id`)=1 THEN ANY_VALUE(`products`.`name`)
ELSE (
SELECT
`name`
FROM
`products` AS `name_sub`
WHERE 1
AND `name_sub`.`group_id`=`products`.`group_id`
AND `name_sub`.`quantity` > `name_sub`.`minimum`
ORDER BY
`name_sub`.`minimum` DESC
LIMIT 1
)
END AS 'name',
## quantity
CASE
WHEN COUNT(`products`.`product_id`)=1 THEN ANY_VALUE(`products`.`quantity`)
ELSE (
SELECT
`quantity`
FROM
`products` AS `quantity_sub`
WHERE 1
AND `quantity_sub`.`group_id`=`products`.`group_id`
AND `quantity_sub`.`quantity` > `quantity_sub`.`minimum`
ORDER BY
`quantity_sub`.`minimum` DESC
LIMIT 1
)
END AS 'quantity',
## minimum
CASE
WHEN COUNT(`products`.`product_id`)=1 THEN ANY_VALUE(`products`.`minimum`)
ELSE (
SELECT
`minimum`
FROM
`products` AS `minimum_sub`
WHERE 1
AND `minimum_sub`.`group_id`=`products`.`group_id`
AND `minimum_sub`.`quantity` > `minimum_sub`.`minimum`
ORDER BY
`minimum_sub`.`minimum` DESC
LIMIT 1
)
END AS 'minimum',
## group_id
`group_id`
FROM `products`
GROUP BY `group_id`
SELECT
CASE
WHEN COUNT(`products`.`product_id`)=1 THEN JSON_OBJECT(
'product_id', ANY_VALUE(`products`.`product_id`),
'name', ANY_VALUE(`products`.`name`),
'quantity', ANY_VALUE(`products`.`quantity`),
'minimum', ANY_VALUE(`products`.`minimum`),
'name', `products`.`group_id`
)
ELSE (
SELECT
JSON_OBJECT(
'product_id', `product_with_required_quantity`.`product_id`,
'name', `product_with_required_quantity`.`name`,
'quantity', `product_with_required_quantity`.`quantity`,
'minimum', `product_with_required_quantity`.`minimum`,
'name', `product_with_required_quantity`.`group_id`
)
FROM
`products` AS `product_with_required_quantity`
WHERE 1
AND `product_with_required_quantity`.`group_id`=`products`.`group_id`
AND `product_with_required_quantity`.`quantity` > `product_with_required_quantity`.`minimum`
ORDER BY
`product_with_required_quantity`.`minimum` DESC
LIMIT 1
)
END AS 'product'
FROM `products`
GROUP BY `group_id`
DELETE FROM `cat_product` WHERE `id` IN (SELECT `inner`.`id` FROM(
SELECT `cat_product`.`id` FROM `cat_product` ORDER BY RAND() LIMIT 1
) as `inner`)
SELECT
ANY_VALUE(id) AS 'id',
office_id,
IF(
POSITION(',' IN GROUP_CONCAT(employee_id))=0,
GROUP_CONCAT(employee_id),
SUBSTR(GROUP_CONCAT(employee_id), 1, POSITION(',' IN GROUP_CONCAT(employee_id))-1)
) AS 'employee_id_first',
IF(
POSITION(',' IN GROUP_CONCAT(employee_id))=0,
NULL,
SUBSTR(
RIGHT(GROUP_CONCAT(employee_id), LENGTH(GROUP_CONCAT(employee_id))-POSITION(',' IN GROUP_CONCAT(employee_id))),
1,
POSITION(',' IN RIGHT(GROUP_CONCAT(employee_id), LENGTH(GROUP_CONCAT(employee_id))-POSITION(',' IN GROUP_CONCAT(employee_id))))-1
)
) AS 'employee_id_second'
FROM
t
GROUP BY
office_id