UPDATE `tasks`
SET @id := `id`, `state` = 'in_work',
`work_start_time` = NOW(), `worker` = :pid
WHERE `state` = 'wait'
ORDER BY `create_time`
LIMIT 1;
SELECT * FROM `tasks` WHERE `id` = @id;
UPDATE `tasks` SET `state` = 'done' WHERE `id` = @id;
@id := `id`
SELECT *
FROM (
SELECT *, MATCH `text` AGAINST('database') AS `score`
FROM `articles`
) AS `t`
WHERE `score` > 0
ORDER BY `source` != 'auto', `score`
SELECT *
FROM (
SELECT *, MATCH `text` AGAINST('database') AS `score`
FROM `articles`
) AS `t`
WHERE `score` > 0
ORDER BY `score`+(`source` = 'auto')*0.5 DESC
SELECT `t1`.`usrid`, `t2`.`data`, `t2`.`postid`
FROM (
SELECT `usrid`, COUNT(*) AS `cnt`
FROM `posts`
WHERE `data` BETWEEN :start AND :end
GROUP BY `usrid`
ORDER BY `cnt` DESC
LIMIT 10
) AS `t1`
JOIN `posts` AS `t2` USING(`usrid`)
WHERE `t2`.`data` BETWEEN :start AND :end
ORDER BY `t1`.`cnt`, `t1`.`usrid`, `t2`.`data`
SELECT `ul`.`user_id`, `ul`.`name`, `ul`.`uid`, `ul`.`gender`
FROM `users_list` AS `ul`
JOIN `users_groups` AS `ug` ON `ul`.`user_id` = `ug`.`user_id`
JOIN `groups_list` AS `gl` ON `gl`.`group_id` = `ug`.`user_id`
WHERE `gl`.`uid` = :gid;
UPDATE `emails` SET `status` = 1 WHERE @id := `id` AND `status` = 0 ORDER BY RAND() LIMIT 1;
SELECT * FROM `emails` WHERE `id` = @id;
SELECT `prod`.`name`, `prod`.`price`,
GROUP_CONCAT(CONCAT(`atr`.`nameAttribute`, '=' , `val`.`attributeValue`) as `attribute`,
FROM `products` as `prod`
LEFT JOIN `productAttribute` as `atr` ON `prod`.`idProduct` = `atr`.`idProduct`
LEFT JOIN `attributeValue` as `val` ON `atr`.`idAttribute` = `val`.`idAttribute`
WHERE `prod`.`idCategory` = :idCategory
GROUP BY `prod`.`idProduct`;
SELECT `u1`.`name`, `u2`.`name`, COUNT(*) AS `count`
FROM `users_groups` AS `ug1`
JOIN `users_groups` AS `ug2`
ON `ug2`.`group_id` = `ug1`.`group_id`
AND `ug2`.`user_id` > `ug1`.`user_id`
JOIN `users` AS `u1`
ON `u1`.`id` = `ug1`.`user_id`
JOIN `users` AS `u2`
ON `u2`.`id` = `ug2`.`user_id`
GROUP BY `ug1`.`user_id`, `ug2`.`user_id`
ORDER BY `count` DESC
LIMIT 1
FROM products prod
INNER JOIN categories cat ON prod.idCategory = cat.idCategory
Это тоже понятно, присоединяется к товару название категории.INNER JOIN productAttribute atr ON cat.idCategory = atr.idCategory
И здесь ясно, присоединяем список атрибутов для этой категории.INNER JOIN attributeValue val ON cat.idCategory = val.idCategory
А вот здесь непонятно, зачем для товара брать все значения атрибутов его категории? Нужно присоединять только значение уже присоединённого атрибута для данного товара. idCategory в таблице attributeValue вообще быть не должно.