SELECT ShippingOffer.shipping_starts_at,
ShippingOffer.station_id,
s.name as StationName --<
FROM ShippingOffer
JOIN ShippingRequest USING (station_id)
LEFT JOIN station as s ON s.id = ShippingOffer.station_id --<
WHERE ShippingOffer.station_id_end = ShippingRequest.station_id_end
CREATE TABLE `TAB001_TYPE` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `TAB002_FIELD` (
`id` INT NOT NULL AUTO_INCREMENT,
`t001_id` INT NOT NULL,
`field_name` TEXT NOT NULL,
`field_note` TEXT NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `TAB004_DOC_VALUE` (
`id` INT NOT NULL AUTO_INCREMENT,
`t003_id` INT NOT NULL,
`t002_id` INT NOT NULL,
`value` varchar NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `TAB003_DOCS` (
`id` INT NOT NULL AUTO_INCREMENT,
`t001_id` INT NOT NULL,
`doc_name` TEXT NOT NULL,
`doc_num` INT NOT NULL,
PRIMARY KEY (`id`)
);
ALTER TABLE `TAB002_FIELD` ADD CONSTRAINT `TAB002_FIELD_fk0` FOREIGN KEY (`t001_id`) REFERENCES `TAB001_TYPE`(`id`);
ALTER TABLE `TAB004_DOC_VALUE` ADD CONSTRAINT `TAB004_DOC_VALUE_fk0` FOREIGN KEY (`t003_id`) REFERENCES `TAB003_DOCS`(`id`);
ALTER TABLE `TAB004_DOC_VALUE` ADD CONSTRAINT `TAB004_DOC_VALUE_fk1` FOREIGN KEY (`t002_id`) REFERENCES `TAB002_FIELD`(`id`);
ALTER TABLE `TAB003_DOCS` ADD CONSTRAINT `TAB003_DOCS_fk0` FOREIGN KEY (`t001_id`) REFERENCES `TAB001_TYPE`(`id`);
IF NOT EXISTS( SELECT NULL
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'price'
AND table_schema = 'db_name'
AND column_name = 'pr1') THEN
ALTER TABLE `price` ADD `pr1` tinyint(1) NOT NULL AFTER `pr`;
END IF;
SELECT T2.*, T1.OPSOS, T1.REGION
FROM TABLE2 T2
LEFT JOIN TABLE1 T1
ON T1.PREFIX = T2.PREFIX
AND T1.PHONE BETWEEN T2.MIN AND T2.MAX
WHERE T2.SITY = 'Москва'
UPDATE lifeusers SET readers = CONCAT(readers, '. ', '$login') WHERE id='$addreader';
SELECT USR1.NAME,
USR1.ID,
C.CNT,
D.EXCNT
FROM USERS USR1
LEFT JOIN (SELECT COUNT (*) AS CNT, OBJ.REALTOR_ID
FROM OBJECTS OBJ
GROUP BY REALTOR_ID) AS C ON USR.ID = C.REALTOR_ID
LEFT JOIN (SELECT COUNT (*) AS EXCNT, OBJ.REALTOR_ID
FROM OBJECTS OBJ WHERE OBJ.EXCLUSIVE = 1
GROUP BY REALTOR_ID) AS D ON USR.ID = D.REALTOR_ID;
WHERE MANAGER_ID IS NULL
ALTER TABLE `Услуги_предприятий`
ADD CONSTRAINT `<CONSTRAINT_NAME1>` FOREIGN KEY (`org_id`) REFERENCES `Предприятия` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `<CONSTRAINT_NAME2>` FOREIGN KEY (`service_id`) REFERENCES `Список_услуг` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
UPDATE my_tab2 as t2
JOIN (SELECT a.region,
a.host
FROM ftpup a,
(SELECT info,
type,
timestamp,
max(date) as time
FROM ftpup
GROUP BY type ) b
WHERE a.type = b.type
AND a.date = b.time
AND a.status <> '0' ) AS t1 ON t1.host = t2.host and t1.region = t2.host
SET t2.status = '2'
INSERT INTO PAGES (ID, CATEGORY_ID, TITLE) VALUES (4, 3, 'title3');
SELECT ID,
CATEGORY_ID,
TITLE
FROM PAGES
WHERE TITLE IN (SELECT TITLE FROM PAGES GROUP BY TITLE HAVING COUNT(*)>1)
GROUP BY TITLE,
CATEGORY_ID
HAVING COUNT(*)=1;
SELECT product_id FROM(
SELECT DISTINCT p.product_id, pd.name, p.model, p.quantity, p.price, p.sort_order, p.date_added , p.price as realprice
FROM oc_product p
LEFT JOIN oc_product_option_value pov ON pov.product_id = p.product_id
LEFT JOIN oc_product_description pd ON pd.product_id = p.product_id
LEFT JOIN oc_product_to_store p2s ON p2s.product_id = p.product_id
LEFT JOIN oc_product_to_category p2c ON p2c.product_id = p.product_id
LEFT JOIN oc_category_path cp ON cp.category_id = p2c.category_id
LEFT JOIN oc_product_attribute p2 ON p2.product_id = p.product_id
WHERE 1 AND cp.path_id IN (3558)
AND (
(p2.attribute_id = 12 AND p2a0.text LIKE '%ЛПО%') OR
(p2a1.attribute_id = 20 AND p2a1.text LIKE '%2х36 Вт%') OR
(p2a2.attribute_id = 22 AND p2a2.text LIKE '%накладны%') OR
(p2a3.attribute_id = 27 AND p2a3.text LIKE '%60 см%') OR
(p2a4.attribute_id = 29 AND p2a4.text LIKE '%T8%') OR
(p2a5.attribute_id = 30 AND p2a5.text LIKE '%220 В%')
)
AND pd.language_id = '1' AND p.status = '1' AND p2s.store_id = 0) as innertable
WHERE 1 ORDER BY sort_order ASC, LCASE(name) ASC LIMIT 0,20
SELECT u.id,
u.name,
IFNULL(SUM(o.amount), 0) AS full_amount,
SUM((SELECT FNULL(SUM(i.qty), 0) FROM `order_items` i where i.order_id = o.id )) full_quantity
FROM `users` u
LEFT JOIN `orders` o ON u.id = o.user_id
--Where кучей фильтров(условий)
GROUP BY u.id, u.name
SELECT DATE_FORMAT(`created_at`, '%y-%m-%d') as`created_at`,
SUM(CASE WHEN updated_at is null THEN 1 ELSE 0 END) AS "created_at_count",
SUM(CASE WHEN updated_at is not null THEN 1 ELSE 0 END) AS "updated_at_count"
FROM `table`
GROUP BY DATE_FORMAT(`created_at`, '%y-%m-%d')
SELECT DATE_FORMAT(`created_at`, '%y-%m-%d') as`created_at`,
SUM(1) AS "created_at_count",
SUM(CASE WHEN updated_at is not null THEN 1 ELSE 0 END) AS "updated_at_count"
FROM `table`
GROUP BY DATE_FORMAT(`created_at`, '%y-%m-%d')
SELECT DATA,
SUM(CASE WHEN USER_ID = 1 AND TYPE = 'add'
THEN 1 ELSE 0 END) AS ADD,
SUM(CASE WHEN USER_ID = 1 AND TYPE = 'remove'
THEN 1 ELSE 0 END) AS REMOVE
FROM ACTIONS
WHERE CREATED_AT > UNIX_TIMESTAMP(DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY))
GROUP BY DATA