SELECT
p.`id`,
p.`name`,
p.`category_id`,
v.`name` AS vendor,
p.`price_ya`,
pp.`value` AS price,
pp.`fix_currency_id`,
ph.`id` AS `photo`,
px.`status`
FROM
`products_prices` AS pp,
`xml_products` AS px
LEFT JOIN (
SELECT
sph.`product_id`,
MIN(sph.`pos`) AS min_pos
FROM `products_photos` AS sph
GROUP BY sph.`product_id`
) AS tph
ON tph.`product_id` = p.`id`
LEFT JOIN `products_photos` AS ph
ON ph.`product_id` = tph .`product_id`
AND ph.`pos` = tph.`min_pos`
LEFT JOIN `vendors` AS v
ON p.`vendor_id`=v.`id`
WHERE
pp.`price_id`='2'
AND p.`id`=pp.`product_id`
AND p.`id`=px.`product_id`
AND px.`site_id`='2'
UPDATE category SET number = 0;
SET @old_category = -1;
SET @num = -1;
UPDATE category AS c
INNER JOIN (
SELECT
t.article_id,
t.sort,
t.category_id,
@old_category,
@num := CASE
WHEN @old_category <> t.category_id THEN 0
ELSE @num + 1
END AS number,
@old_category := t.category_id
FROM (
SELECT
a.article_id,
a.sort,
c.category_id
FROM category AS c
INNER JOIN article AS a ON a.article_id = c.article_id
ORDER BY c.category_id, a.sort
) AS t
) AS t
ON t.article_id = c.article_id
SET c.number = t.number;
SELECT c.*, a.sort FROM category AS c
INNER JOIN article AS a ON a.article_id = c.article_id
ORDER BY c.category_id, a.sort
aricle_id | category_id | number | sort 5 | 1 | 0 | 4 3 | 1 | 1 | 7 4 | 2 | 0 | 2 1 | 2 | 1 | 3 2 | 2 | 2 | 5
CREATE DEFINER = 'root'@'localhost' TRIGGER `tab_before_ins_tr` BEFORE INSERT ON `tab`
FOR EACH ROW
BEGIN
SET NEW.Total_in_USD = NEW.quantity * NEW.USD;
END;
CREATE DEFINER = 'root'@'localhost' TRIGGER `tab_before_upd_tr` BEFORE UPDATE ON `tab`
FOR EACH ROW
BEGIN
SET NEW.Total_in_USD = NEW.quantity * NEW.USD;
END;
SELECT
t.city_id,
COALESCE(cru.lang_id, cen.lang_id) AS lang_id,
COALESCE(cru.`name`, cen.`name`) AS `name`
FROM (
SELECT city_id FROM city
WHERE lang_id IN('ru', 'en')
GROUP BY city_id
) AS t
LEFT JOIN city AS cru ON cru.city_id = t.city_id AND cru.lang_id = 'ru'
LEFT JOIN city AS cen ON cen.city_id = t.city_id AND cen.lang_id = 'en'
INSERT INTO tabl2 (login, top)
SELECT * FROM (SELECT login, COUNT(*) AS cnt FROM tabl1 GROUP BY login) AS t1
ON DUPLICATE KEY UPDATE top = t1.cnt;