select distinct vegetables.name
from vegetables, deliveries, routes, pathes
where
deliveries.vegetable = vegetables.id and
routes.id=deliveries.route and
pathes.route=deliveries.route and
exists (
select * from pathes, points
where pathes.route=route.id and
from pathes.fromPoint=points.id and points.Name='Москва') and
exists (
select * from pathes, points
where pathes.route=route.id and
from pathes.toPoint=points.id and points.Name='Тюмень')
select * from table where instr ( concat(';',addr,';'),';Москва;') > 0
select * from table where instr ( concat(';',lang,';'),';рус;') > 0
select * from table where instr ( concat(';',times,';'),';пт;') > 0
WITH
RECURSIVE search_graph(id, ref_id) AS
(
SELECT id, ref_id
FROM Users
UNION ALL
SELECT o.id, o.ref_id
FROM Users o
JOIN search_graph p ON p.ref_id = o.id
)
SELECT array_agg(id || ' -> ' ) AS "path"
FROM search_graph
GROUP BY id
ORDER BY id
;
select t2.* from table1 t1,table1 t2
where
t1.from_user_id=t2.to_user_id and
t2.from_user_id=t1.to_user_id and
t1.from_user_id=5
select BSproduct from
( -- Список продуктов имеющих черный цвет и малый размер
select product as BSProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where color = "black" and size = "S"
) as BlackAndSmallSize,
( -- Список продуктов имеющих черный цвет и средний размер
select product as BMProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where color = "black" and size = "M"
) as BlackAndMidSize,
( -- Список продуктов имеющих большой размер
select product as LProduct from
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
where size = "L"
) as LargeSize
where BSProduct=BMProduct and BMProduct=LProduct
(
SELECT t1.id, t1.name AS product, t3.name AS color, t5.name AS size
FROM `product` AS t1
JOIN `product_color` AS t2 ON t1.id = t2.product_id
JOIN `color` AS t3 ON t2.color_id = t3.id
JOIN `product_size` AS t4 ON t1.id = t4.product_id
JOIN `size` AS t5 ON t4.size_id = t5.id
)
DELIMITER $$
CREATE TRIGGER auto_inc_new_admin_logs
BEFORE INSERT ON new_admin_logs FOR EACH ROW
BEGIN
DECLARE new1 INT;
SELECT max(id) into new1 FROM new_admin_logs;
SET NEW.id = new1 + 1;
END$$
CREATE TRIGGER `update_users` AFTER UPDATE ON `users`
FOR EACH ROW
BEGIN
INSERT INTO payments(status_id, user_id, description, currency_id,
amount, bonus_amount, sum)
values('2' ,new.id, 'test', 643,
old.balance, old.balance+new.balance, new.balance);
END;
select ads.id, ads_meta.text, ads_images.path ...
from ads, ads_images, ads_meta
where ads_images.ads_id=ads.id and ads_meta.ads_id=ads.id
order by ads.id
UPDATE table1 SET col1 = col1+1
update table1 set col1=1 where col is null
, если второе - хватит и первой команды. select product_id as id, products.name,
group_concat(sizes.value separator ';') as sizes
from product_sizes, sizes, products
where product_id=products.id and size_id=sizes.id
group by product_id
select products.id, products.name,
group_concat(sizes.value separator ';') as sizes
from product_sizes
inner join sizes on (size_id=sizes.id)
right join products on (product_id=products.id)
group by product_id order by id;
`Phone` varchar(15) NOT NULL
`Phone` char(15) NOT NULL
или (если не хранятся в значениях нецифровые символы - скобки, например)`Phone` int NOT NULL